Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

The vim editor: a quick cheat sheet

How to open a file, make a change, save and quit; moving around, copying, searching and replacing in vim.

Contents

vi or vim is available on almost every server, so it is worth knowing the basics. If vim feels unfamiliar, you can edit files in nano instead: nano file — the key hints are shown at the bottom of the screen. If nano is not installed, run sudo apt install nano or sudo dnf install nano.

Modes

You open a file with vim file in normal mode: here the keys are commands (moving, deleting, copying), not text. To type, switch to insert mode:

  • i — insert before the cursor, a — insert after it;
  • o — a new line below, O — a new line above.

Esc takes you back to normal mode — if you get lost, press it a couple of times. A colon in normal mode opens the command line at the bottom of the screen: saving, quitting, replacing.

Save and quit

:wsave
:wq or :xsave and quit
:qquit, if there are no changes
:q!quit without saving
:w !sudo tee % >/dev/nullsave a file that you opened without sudo and cannot write to

Moving around

arrow keys or h j k lleft, down, up, right
w / bforward / back one word
0 / $start / end of the line
gg / Gstart / end of the file
:42 or 42Gline 42
Ctrl+F / Ctrl+Bpage down / up

Deleting, copying and pasting

xdelete the character under the cursor
dd, 5dddelete a line, five lines (what you delete can be pasted)
dwdelete a word
yy, 5yycopy a line, five lines
p / Ppaste below / above
Jjoin the line with the next one
u / Ctrl+Rundo / redo

Searching and replacing

/textsearch forwards; n — next match, N — previous match
:s/old/new/greplace all matches in the current line
:%s/old/new/greplace throughout the file
:%s/old/new/gcthe same, asking for confirmation of each replacement
:10,20s/^/#/comment out lines 10 to 20

Useful settings

:set number / :set nonumbershow / hide line numbers
:set pastepaste text from the clipboard without a “staircase” of indentation; afterwards, run :set nopaste
:syntax onsyntax highlighting

Didn’t find the answer? Ask our support team — we’ll help.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies