vi/vim essentials

Moving

    ^
    k
< h    l >
    j
    v

or cursor keys (but try to learn hjkl)

Quitting

:q
:q!  (quit ignoring changes)

Modes

  1. Normal/Command mode (ESC)

  2. Insert mode – INSERT –

    i     insert before cursor

Note

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:

i

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:

A

delete character:

x

delete whole line:

dd

delete word:

dw

delete to end of line:

d$

(see more near bottom under Operators and motions)

Undo

u

(can undo multiple times)

Saving file

save the file you opened:

:w

save and quit:

:wq

or

ZZ

save file under new name:

:w filename

More editing

put (paste) command AFTER cursor:

p

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>:

r<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:

gg

end of file

G

page up:

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

page down:

CTRL+B
PgUp

Searching

search for a <phrase> forward:

/<phrase>

Hit / (followed by RETURN) to find more.

search backward:

?<phrase>

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

:s/old/new

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

:s/old/new/g

To substitute all occurrences in the file type

:%s/old/new/g

To ask for confirmation each time add ‘c’

:%s/old/new/gc

Note

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:

O
o

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

where

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 $

Thus

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

Indentation

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:

:r <FILENAME>

Run a shell command <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:

CTRL+g

Join two lines like these:

J
:j

Customization

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

~/.vimrc

Example (for python) between the —– lines:

-----------------[BEGIN]------------
" vim configuration file for python editing
" much more on http://stackoverflow.com/questions/164847/what-is-in-your-vimrc

syntax on
" colorscheme for light bg (see eg
" http://vimcolorschemetest.googlecode.com/svn/html/index-pl.htm and
" http://vim.wikia.com/wiki/Switch_color_schemes)
set t_Co=256
colorscheme default

" Python: see  http://wiki.python.org/moin/Vim
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

-------------------------[END]----------

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)

HELP!!

Help:

:help

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

vimtutor

and follow the instructions.

Quick reference “cheat” sheet: http://tnerual.eriogerg.free.fr/vimqrc.pdf (or download the local copy of vimqrc.pdf)