vi/vim essentials


< h    l >

or cursor keys (but try to learn hjkl)


:q!  (quit ignoring changes)


  1. Normal/Command mode (ESC)

  2. Insert mode – INSERT –

    i     insert before cursor


ESC always gets you back to Normal Mode

ESC can also be used to cancel a command.

Editing text

switch to insert mode and insert at cursor:


Now enter text. Backspace typically works. Text can, obviously, span multiple lines. Cursor keys might or might not work as expected in insert mode. Now go back to normal mode by pressing ESC.

switch to insert mode and append at end of line:


delete character:


delete whole line:


delete word:


delete to end of line:


(see more near bottom under Operators and motions)



(can undo multiple times)

Saving file

save the file you opened:


save and quit:




save file under new name:

:w filename

More editing

put (paste) command AFTER cursor:


2) delete this line with dd and put it after 1) 1) first line (position cursor anywhere in this line, then p) 3) of third line beginning

(Also delete word ‘beginning’ with dw and put it at the beginning of the sentence of line 3. Cursor must be on space!)

replacing a character <c>:


Marking text for copying:

v    (enters -- VISUAL -- mode)

now move around with the movement commands to highlight the text you want to manipulate

d    (delete = cut the highlighted text)
y    ("yanks" = copies the text)

(these commands terminate VISUAL mode)

Now insert the text with the p (put = paste) command.

More movement

moving the cursor along words

beginning of each word:

w    (forward)
b    (backward)

end of each word:

e    (forward)
ge   (backward)

Beginning of this line:

^    (caret character)

End of line:


beginning of file:


end of file


page up:

CTRL+F    (= CTRL+SHIFT+f all at the same time)

page down:



search for a <phrase> forward:


Hit / (followed by RETURN) to find more.

search backward:


Hit ? (followed by RETURN) to find more. Hit / to search forward again.

Get back to where you started searching from (can be used repeatedly):

CTRL+o    (CONTROL and 'o' at the same time)

(also try CTRL+i to go forward in the search matches)

Matching parentheses/brackets search (good for programming!):

%    (while cursor is on (,),[,],{, or })

Search and Replace

To substitute new for the first old in a line type


To substitute new for all ‘old’s on a line type


To substitute all occurrences in the file type


To ask for confirmation each time add ‘c’



The pattern old is actually a basic regular expression, thus characters such as ., *, ^, $, [, and ] have a special meaning and are not interpreted literally. They need to be backslash-quoted. E.g. search for double asterisk: :%s/\*\*/new/g

See the quick reference for details and ‘man re_format’

More on inserting/replacing

insert a new line above/below a line:


overwrite (replace) characters:

R   (starts -- REPLACE -- mode)

change word/line/...

cw  (delete word and INSERT)
c$  (delete up to end of line and INSERT)
c^  (delete to beginning of line and INSERT)

Using c is faster than using d i (delete, then insert) — it makes a difference once you get used to it.

Operators and motions

A change command (such as d, c) consists of

operator [number] motion


operator    - what to do, e.g. d for delete
[number]    - optional count to repeat the motion
motion      - how far to go. A motion can be one of the
              movements such as w or e but also search
              expressions /<phrase> or end of line $


2w           move cursor two words forward (empty operator
             is the cursor movement)
10dd         delete next 10 lines
5dw          delete next 5 words
 d2j         delete current and next 2 lines
 y/XXX       yank up to the text XXX but not further
 y$          yank until end of line


When editing source code (such as Python files) you should enable autoindent mode (:set autoindent) e.g. in your ~/.vimrc. You can move along the indent levels with:

CTRL+d     move one indent level to left
CTRL+t     move one indent level to right

Other stuff

Read a file <FILENAME> and put it below the cursor:


Run a shell command <COMMAND>:


e.g. :!ls.

:r !ls also works, i.e. you can put the output of a command directly into your edited file.

Display the line number in the file:


Join two lines like these:



The :set command is used to switch on and off many wonderful features. Put your personal set commands into the configuration file


Example (for python) between the —– lines:

" vim configuration file for python editing
" much more on

syntax on
" colorscheme for light bg (see eg
" and
set t_Co=256
colorscheme default

" Python: see
autocmd BufRead,BufNewFile *.py syntax on
autocmd BufRead,BufNewFile *.py set ai
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class

" indentation
" add to sourcefiles:
"  # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
set modeline
au FileType python setl autoindent tabstop=4 expandtab shiftwidth=4 softtabstop=4


You can write the part between the lines to a file:

  1. position cursor on the first line below the BEGIN line

  2. v

    (enter VISUAL mode)

  3. move to blank line before bottom END marker line

  4. :

    (use highlighted region for next command)

  5. w ~/.vimrc

    (write highlighted region to file, overwrites any previous version)




The vim tutorial: If you have time or want more practice the run the interactive tutorial that comes with vim. It takes 20-30 Minutes. In the shell, enter


and follow the instructions.

Quick reference “cheat” sheet: (or download the local copy of vimqrc.pdf)