Vim (and its older form, vi) is installed on almost every Linux server, including minimal ones and rescue environments where nothing else is available. It looks confusing at first because typing does not insert text straight away. Once you understand its modes, a dozen keys are enough to edit, save and quit with confidence.
Before you start
- SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
- On a minimal install, only
vimay be present. Every key in this guide works in bothviandvim. To install full Vim on Ubuntu or Debian:
sudo apt install vimOn AlmaLinux or Rocky Linux:
sudo dnf install vim-enhancedUnderstand the modes
Vim has several modes. You need three:
- Normal mode: the mode Vim starts in. Keys are commands (move, delete, undo), not text.
- Insert mode: keys type text, like a normal editor. The bottom line shows
-- INSERT --. - Command-line mode: you type a command after
:at the bottom of the screen, for example to save or quit.
The golden rule: press Esc to get back to normal mode. If you are ever unsure what mode you are in, press Esc once or twice. It is always safe.
Open a file
vim notes.txtTo open a system file, use sudo:
sudo vim /etc/ssh/sshd_configAdd +42 to jump straight to line 42:
vim +42 /etc/nginx/nginx.confInsert text
From normal mode, press one of these to start typing:
- i: insert before the cursor.
- a: append after the cursor.
- o: open a new line below and start typing.
- O: open a new line above.
Type your text, then press Esc to go back to normal mode.
Save and quit
In normal mode, type a colon command and press Enter:
:wsaves (writes) the file.:qquits. Vim refuses if there are unsaved changes.:wqsaves and quits.:xandZZdo the same, but only write if something changed.:q!quits without saving, throwing away your changes.
Tip: If you made a mess and want to start over, press Esc, type :q! and press Enter. The file stays exactly as it was when you last saved.
Move around
The arrow keys, Page Up and Page Down work in most terminals. The classic Vim keys are worth learning because they work everywhere:
- h j k l: left, down, up, right.
- w / b: next word / previous word.
- 0 / $: start / end of the line.
- gg / G: first / last line of the file.
- 42G or
:42: go to line 42.
Turn on line numbers with :set number and off again with :set nonumber.
Delete, copy, paste and undo
All of these are typed in normal mode:
- x: delete the character under the cursor.
- dd: delete (cut) the whole line. 3dd cuts three lines.
- dw: delete to the end of the word.
- yy: copy ("yank") the line. 3yy copies three lines.
- p: paste below the cursor. P: paste above.
- u: undo. Press it repeatedly to undo more.
- Ctrl + R: redo.
- .: repeat the last change.
Search and replace
- Type
/wordand press Enter to search forward. n jumps to the next match, N to the previous one. ?wordsearches backward.:%s/old/new/greplaces everyoldwithnewin the whole file.:%s/old/new/gcdoes the same, but asks you to confirm each change with y or n.
Paste from your computer without a mess
When you paste a block of text from your PC into Vim over SSH, auto-indent can add extra spaces on every line. Recent Vim versions handle this automatically in most terminals. If you see a staircase of indentation, undo with u, then:
- Type
:set pasteand press Enter. - Press i, paste with your terminal's paste shortcut, and press Esc.
- Type
:set nopasteand press Enter.
Edit configs safely
The same routine as with any editor applies:
- Back up the file:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. - Edit and save with
:wq. - Test the config before restarting the service, for example
sudo sshd -torsudo nginx -t.
Warning: An error in SSH, firewall or network config can lock you out. Keep your current session open while testing from a new one. If you do get locked out, use the browser console in your VPS provider's control panel.
Check it works
Practise on a scratch file:
- Run
vim ~/practice.txt. - Press i, type
hello vim, and press Esc. - Type
:wqand press Enter. - Run
cat ~/practice.txt. You should seehello vim.
For a guided tour, run vimtutor. It takes about half an hour and teaches the basics hands-on.
Troubleshooting
- Typing does nothing or does strange things: you are in normal mode. Press i to insert text.
E37: No write since last change: you tried:qwith unsaved changes. Use:wqto save or:q!to discard.E45: 'readonly' option is setorCan't open file for writing: you opened a root-owned file withoutsudo. Save a copy with:w ~/file.new, quit, and copy it into place withsudo cp. Or, in Vim,:w !sudo tee %writes it with sudo.Found a swap filewarning on open: Vim crashed or the file is open in another session. Choose (R)ecover to restore unsaved work, or (D)elete the swap file if you know the other session is gone.- The screen froze: you may have pressed Ctrl + S. Press Ctrl + Q to resume.
Next steps
- Prefer something simpler? Use nano.
- Find help for any command: Get help in Linux with man pages.
- Keep copies before you edit: Back up config files before editing.
