Saturday, February 21, 2004

Vi & Vim Guide

Vi is a small, fast and efficient text editor that is installed by default in almost any Unix or Linux system, and Vim (Vi IMproved) is an enhanced version of vi. Most people find vi somewhat hard to learn at first, but it enables fast, simple and effective editing once you get used to it. I got hooked to vi when I first worked with Sun Sparc10 workstation in 1995. Vi is extremely powerful in moving around files without touching any arrow keys. Vi operates in three modes: command mode (colon prompt), insert mode and default mode. For insert mode, press ESC and type colon to switch to command mode. Vi works in default mode after ESC is pressed in all modes (without input colon). In default mode, we are able to move around in the file, switch to insert mode or command mode.

Follow is the vi/vim commands I most use:

























































































































































































Command


Mode


Description







vi filename
default


start editing a file
:wq command


write to disk and quit ( equal to :x)
:q! command


quit without saving any changes
:e! command recover to the original without any changes
:!command command execute a command; :!sh fork a shell, Ctrl-d
to exit






h; l; k; j
default one space to the left, right; one line up,
down respectively
^; $ default the beginning or end of current line
w; e default the beginning or end of next word
Ctrl-b; Ctrl-f default one page up (backward) or down
(forward)
:number command move line with the number
Ctrl-L command clear and redraw the screen
G default move to the end of file
/string; n default search string and repeat search (next)
a; A default append from next letter or the end of current
line
i; I default insert from current position or the beginning
of current line
o; O default open new line down or up of current line
x default delete single character; 5x deletes 5
characters
dw; dd default delete word or linde; 5dw deletes 5 words and
5dd deletes 5 lines
yy default yank (copy) into buffer; 5yy copies 5 lines
into buffer
p; P default paste buffer to next or previous line
u; U default undo last change or restore the current line
:set num; :set nonum command turn on or off line numbering
:set ic; set noic command ignore (or not ignore) case when searching
ma; :'a; y'a; d'a default

command
set marker 'a' (could be 'a' to 'z') to the
current position, go to marker 'a', copy text from current position to
marker 'a' into buffer, delete text from current position to marker 'a'
:%g/^#/d;


:%s/^#//g
command delete all comment lines or uncomment all the
comment lines
:[10-20]g/hello/d;


:[10-20]v/hello/d
command delete all lines containing (or not
containing) text "hello" from line 10 to 20
:i,jd;


:'a,'bd
command delete text from line i to j or from marker
'a' to marker 'b' inclusively
:i,js/old text/new text/g command substitute "new text" for "old text" from
line i to j






:'a,'bs/old text/new text/g





command substitute texts from marker 'a' to marker
'b'
:25,30m50;



'a,'bm50;


25,30co50
command move lines 25-30 to a new position after line
50; move the text between markers to line after 50; copy lines 25-30 to
line after 50