> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Edit Files in the Linux Terminal with nano and vim

> Edit files in the Linux terminal: open with sudo nano file, save with Ctrl+O and exit with Ctrl+X. In vim press i to type, Esc then :wq to save and quit.

To edit a file in the Linux terminal, open it with `sudo nano /etc/example.conf`, make your change, press `Ctrl+O` then `Enter` to save and `Ctrl+X` to exit. In vim, run `sudo vim /etc/example.conf`, press `i` to start typing, then press `Esc` and type `:wq` and `Enter` to save and quit, or `:q!` to quit without saving.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 10 minutes | Every Linux distribution | An SSH session |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md). System files in `/etc` belong to root, so open them with `sudo` (root can leave it out).
* Copy a config file before you change it, so you can go back:
  ```bash theme={null}
  sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  ```

## Which editor should I use?

Use **nano** if you are new to Linux: the shortcuts are listed at the bottom of the screen. Use **vim** if you already know it or want a powerful editor; `vi` or `vim` is present on almost every server, including minimal installs and rescue systems.

If an editor is missing, install it:

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt install nano vim
> ```

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo dnf install nano vim-enhanced
> ```

## How to edit a file with nano

**1. Open the file**

```bash theme={null}
sudo nano /etc/hosts
```

If the file doesn't exist, nano creates it when you save.

**2. Make your change**

Move with the arrow keys and type normally. Paste text you copied on your computer with your terminal's paste (usually right-click, or `Ctrl+Shift+V`).

**3. Save**

Press `Ctrl+O`, check the file name at the bottom, and press `Enter`. `Ctrl+S` saves without the question.

**4. Exit**

Press `Ctrl+X`. If there are unsaved changes, nano asks: press `Y` to save or `N` to discard.

### nano shortcuts

In nano's help bar, `^` means `Ctrl` and `M-` means `Alt`.

| Shortcut | Action |
| - | - |
| `Ctrl+O` | Save (Write Out) |
| `Ctrl+X` | Exit |
| `Ctrl+W` | Search (Where Is); press `Alt+W` for the next match |
| `Ctrl+\` | Search and replace |
| `Ctrl+K` | Cut the current line |
| `Ctrl+U` | Paste |
| `Alt+U` / `Alt+E` | Undo / redo |
| `Ctrl+_` | Go to a line number |
| `Ctrl+C` | Show the current line number |
| `Ctrl+G` | Help |

To open a file at a line, for example line 42, use `nano +42 /etc/ssh/sshd_config`. Show line numbers with `nano -l`.

## How to edit a file with vim

vim has **modes**. It opens in normal mode, where keys are commands. You type text only in insert mode.

**1. Open the file**

```bash theme={null}
sudo vim /etc/hosts
```

**2. Go to insert mode**

Move to the right place with the arrow keys and press `i`. The bottom line shows `-- INSERT --`. Now type your change.

**3. Go back to normal mode**

Press `Esc`.

**4. Save and quit**

Type `:wq` and press `Enter`.

### How to exit vim

| Type (after `Esc`) | Result |
| - | - |
| `:wq` or `:x` | Save and quit |
| `:w` | Save, keep editing |
| `:q` | Quit (only works with no unsaved changes) |
| `:q!` | Quit and throw away changes |

### vim commands (normal mode)

| Keys | Action |
| - | - |
| `i` / `a` / `o` | Insert before cursor / after cursor / on a new line |
| `/text` then `n` | Search, then next match |
| `:%s/old/new/g` | Replace every `old` with `new` |
| `dd` | Delete (cut) a line |
| `yy` / `p` | Copy a line / paste below |
| `u` / `Ctrl+R` | Undo / redo |
| `gg` / `G` | Go to first / last line |
| `:42` | Go to line 42 |
| `:set number` | Show line numbers |

New to vim? Run `vimtutor` for a 30-minute interactive lesson.

## How to check it worked

Print the file to confirm your change was saved:

```bash theme={null}
grep -n "example.com" /etc/hosts
```

For service configs, run the service's own syntax check before restarting it, for example `sudo nginx -t` or `sudo sshd -t`. These print nothing (sshd) or `syntax is ok` (nginx) when the file is valid.

> [!WARNING]
>
> A mistake in `/etc/ssh/sshd_config`, `/etc/fstab` or a firewall file can lock you out or stop the server from booting. Run the syntax check, keep your SSH session open while you test, and keep the [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md) and [rescue mode](https://help.freakhosting.com/portals/vps-panel/how-to-use-rescue-mode.md) in mind as a way back in. Edit `/etc/sudoers` only with `sudo visudo`.

## How to change the default editor

Programs such as `crontab -e`, `visudo` and `git` open your default editor.

```bash theme={null}
# Ubuntu / Debian: choose the system default
sudo update-alternatives --config editor

# Any distribution: set it for your user
echo 'export EDITOR=nano VISUAL=nano' >> ~/.bashrc
source ~/.bashrc
```

## Frequently asked questions

### I'm stuck in vim. How do I get out?

Press `Esc` two or three times, type `:q!` and press `Enter`. This quits without saving.

### nano says "Permission denied" when I save. Why?

You opened a file that belongs to root without `sudo`. Exit with `Ctrl+X`, answer `N`, and open it again with `sudo nano`. Or save a copy to your home folder with `Ctrl+O` and a new name.

### What is the ".swp" warning when I open a file in vim?

vim found a swap file from an editing session that crashed or is still open. If nobody else is editing the file, press `R` to recover or `D` to delete the swap file, then continue.

### Can I edit server files on my own computer instead?

Yes. Use an SFTP client such as WinSCP or FileZilla; see [transfer files with SFTP](https://help.freakhosting.com/portals/other/how-to-transfer-files-with-sftp-winscp-and-filezilla.md). Check that it saves with Linux line endings (LF).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Essential Linux commands](https://help.freakhosting.com/portals/other/essential-linux-commands-for-server-beginners.md)
>
> The commands you use every day.

> [card] [Schedule tasks with cron](https://help.freakhosting.com/portals/other/how-to-schedule-tasks-with-cron.md)
>
> Uses your default editor.

> [card] [Environment variables and .bashrc](https://help.freakhosting.com/portals/other/how-to-use-environment-variables-and-bashrc.md)
>
> Edit your shell settings.

***

> [!NOTE]
>
> **Checked against the official [GNU nano cheat sheet](https://www.nano-editor.org/dist/latest/cheatsheet.html) and the [Vim documentation](https://vimhelp.org/)** on 4 November 2026; tested with nano 7.2 and Vim 9.1 on Ubuntu 24.04 LTS. Shortcuts can differ in other versions.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.