Basic vim commands
Updated 26 Feb 2014
Starting vim *
Command mode *
Insert mode *
Visual mode *
Ex mode *
vimdiff
vim is a very powerful and highly configurable text editor. Once installed, the config file is /etc/vimrc. You can edit this to enable all sorts of fancy things, or use a ready-made config file such as this one.
Open a file in vim:
vim somefile.txt
vim has four different modes:
- Command mode: keystrokes are interpreted as commands
- Insert mode: keystrokes are entered into the file
- Visual mode: keystrokes select, cut, copy and paste text
- Ex mode: input mode for additional commands e.g. saving a file, replacing text, etc.
Access or return to command mode at any time by pressing Esc.
The editor always starts in command mode. Commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them subsequently switch the editor into insert mode. All commands are case sensitive. Sometimes the uppercase versions are blunter versions (s will replace a character, whereas S will replace a line), other times they are completely different commands (j will move down, but J will join two lines).
You can move the cursor with the arrow keys, but this isn't the vim way. You'd have to move your right hand from the standard typing position all the way to the arrow keys, and then back. So...
- h moves the cursor to the left
- l moves it to the right
- k moves up
- j moves down
These commands will quickly move you around the screen:
- H moves to the top of the screen
- M goes to middle-screen
- L goes to bottom-screen
Here are some must-learn commands for quickly navigating around your text:
- $ gets you to the end of the line
- ^ gets you to the first non-whitespace in the line
- 0 (zero) gets you to the very beginning of the line, including whitespace
- w advances a word or special character
- W is the same as w except more characters are included in what vim thinks of as a word
- e advances to the end of a word, likewise E as the counterpart of W
- b goes back a word to the beginning of the word, likewise B as the counterpart of W
- ( goes to the beginning of the sentence, or what vim thinks is a sentence, ) moves to the end of the sentence
- { goes to the beginning of the paragraph, } to the end
- gg goes to the beginning of the file, G to the end
- CTRL-D scrolls down page by page
Search for strings in your text:
- /astring will search the string in the file and position the cursor on the first match below its position
- / will perform the same search again, moving the cursor to the next match
The following commands do basic editing without leaving command mode:
- x is delete, X is backspace
- dd deletes the current line
- yy yanks the entire line, even if it's not all selected
- p pastes after the cursor, P before
- u is undo
- CTRL-r is redo
Commands in vim can be juxtaposed together into something more powerful. For example, 2} will advance the cursor two paragraphs while 6x deletes the next six characters. d tells vim that you want to delete something. Here are some possible ways to use d:
- dW will delete up to the next word
- 3dW deletes the next three words
- d$ deletes from the cursor to the end of the line (same as D)
- d^ will delete from the beginning of the line, after whitespace, to the cursor
- d0 will delete from the very beginning of the line to the cursor
- 4dd deletes four entire lines starting from the current line
Note that deleted text is saved to the clipboard, so d can be used to cut text.
The following commands move the cursor and then switch directly into insert mode:
- a appends: it moves the cursor one position to the right before switching to insert mode, A does so at the end of the current line
- i switches to insert mode at the current cursor position, I at the beginning of the line
- o inserts a blank line under the current cursor position, moves the cursor to that line and switches to insert mode
- O inserts a blank line ABOVE the current line, moves to that line and switches to insert mode
In insert mode you type in your text like in any other word processor. vim can be configured with all sorts of useful options and plugins to better the experience, such as code completion, syntax highlighting and much more.
Pressing v from command mode will put you into visual mode. Here you can move around to select text. Pressing V instead will put you into Visual Line mode. It works much the same way except that entire lines are automatically selected as you move the cursor up or down. Ctrl+v puts you into Visual Block mode. This time blocks of text are automatically selected as you move up or down.
Once you have selected some text:
- y to yank (copy) the selected text into the buffer (clipboard)
- c to cut
To paste, make sure you're in command mode then press p to paste after the cursor, or P to paste before.
Visual mode is good for selecting specific text to cut or copy. For basic cutting, d is probably faster (whenever you delete something, that something is placed inside a buffer and is available for pasting).
Ex mode commands begin with a colon. Here are the most commonly used:
- :w will save (write) the file
- :q will exit the editor
- :wq will save and exit
- :q! forces the exit when you want to quit a file containing unsaved changes
- :w newfile will save the text to newfile
- :wq! overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account)
- :set number shows line numbers, :set nonumber to turn it off
- :n moves to line n of the file
- :1, $s/word/anotherword/g will replace word with anotherword throughout the file
- :recover will recover a file after an unexpected interruption
One really cool feature of vim is its diff editor. vimdiff opens a horizontally multi-paned view that colourfully highlights differences between files, each pane containing one of the files to be examined/edited. Start vimdiff from the command line:
sudo vimdiff file1 file2
And here are the most important commands specific to vimdiff:
- CTRL-w-w to switch panes
- ]c goes to next difference
- [c goes to previous difference
- do (diff obtain) copies the differences from the other pane to the current pane
- dp (diff put) copies differences from the current to the other pane
- zo opens folded text
- zc closed folded text
- :diffupdate re-scans for differences