Linux VPS

Edit files on a Linux server with nano and vim

Open, edit, save and quit configuration files on your Linux VPS with nano for simple edits and vim for power users, with the key shortcuts you actually need.

BeginnerLinux VPS2 min read

Most server configuration lives in plain text files, so editing over SSH is a core skill. nano is the easiest editor and shows its shortcuts on screen; vim is installed almost everywhere and is fast once you know a handful of keys.

Before you start

  • An SSH session (see connect with SSH).
  • sudo rights to edit system files under /etc.
  • Install the editors if missing: sudo apt install nano vim or sudo dnf install nano vim.

Back up the file first

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Edit with nano

sudo nano /etc/hosts

Type to edit. The bottom bar lists shortcuts, where ^ means Ctrl:

KeysAction
Ctrl+O, EnterSave
Ctrl+XExit (asks to save if changed)
Ctrl+WSearch
Ctrl+K / Ctrl+UCut line / paste
Ctrl+_Go to line number
Alt+UUndo

Edit with vim

sudo vim /etc/hosts

vim opens in normal mode, where keys are commands. Press i to enter insert mode and type; press Esc to return to normal mode.

Keys (normal mode)Action
:w EnterSave
:wq EnterSave and quit
:q! EnterQuit without saving
/word Enter, then nSearch, next match
dd / pDelete line / paste
u / Ctrl+RUndo / redo
gg / G / :42Top / bottom / line 42

Tip: Stuck in vim? Press Esc twice, type :q! and Enter to leave without changes.

Pick your default editor

Commands such as crontab -e and visudo use your default editor. Set it with export EDITOR=nano in ~/.bashrc, or on Ubuntu run sudo update-alternatives --config editor.

After editing

Test the config before reloading the service, e.g. sudo nginx -t, sudo sshd -t or sudo apachectl configtest, then reload as shown in systemctl and journalctl.

Troubleshooting

  • File is read-only / Permission denied on save: you opened it without sudo. In vim you can still save with :w !sudo tee %.
  • Swap file found in vim: another session is editing the file or one crashed. Choose (R)ecover or delete the .swp file if nobody else is editing.
  • Pasted text gets auto-indented in vim: run :set paste before pasting.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.