Advanced editing using vi This file gives a more detailed description of the vi command set.
INSERTING TEXT
To leave the Insert mode, press 'Esc'
a Append after cursor A Append after end of current line i Insert before cursor
I Insert before beginning of current line o Open new line below current line and insert
O Open new line above current line and insert CTRL-vchar While inserting, ignore special meaning of char
(e.g., for inserting characters like Esc and control characters)
:r file Read file, and insert after current line J Join next line to end of current line
DELETING TEXT
CTRL-h or
Backspace While in Input Mode delete previous character
Typing a number before these commands will delete that amount of characters, words, or lines.
x Deletes current character dw Delete current word dd Deletes current line
:n,md Deletes lines n through m D or d$ Delete from cursor to end of current line
dcursor_cmd Delete text to cursor to cursor_cmd(e.g.,dG deletes from current line to end of file)
UNDOING AND REPEATING COMMANDS
u Undo last command U Restore current line to original state
"np Retrieve last nth delete(last 9 deletes are in a buffer)" n Repeat last / or ? search command
N Repeat, in reverse direction, last / or ? search command ; (semi-colon) Repeat last f, F, t or T search command
, (comma) Repeat, in reverse direction, last f, F, t or T search command . (period) Repeat last text change command
MOVING THE CURSOR
k or CTRL-p Up j or CTRL-j or CTRL-n Down h or CTRL-h or Backspace Left l (lower case L)
or Spacebar Right w or W Start of next word; W ignores punctuation b or B Start of previous word; B ignores punctuation
e or E End of next word; E ignores punctuation 0 (zero) or | (pipe) First column in current line n| Column n in current line
^ (caret) First non-blank character in current line $ Last character in current line + or Return First character in next line
- First non-blank character in previous line 1G First line in file
G Last line in file G$ Last character in file nG Line n in file
( Back to beginning of sentence ) Forward to beginning of next sentence
\{ Back to beginning of paragraph \} Forward to beginning of next paragraph
PATTERN MATCHING
Pattern matching characters help find strings with similar char- acteristics.
:set magic Allow pattern matching with special characters
(default) :set nomagic Allow only ^ and $ as special characters ^ (caret) Match beginning of line
$ Match end of line . (period) Match any single character \< Match beginning of word
\> Match end of word [str] Match any single character in str [^str] Match any character not in str
[a-n] Match any character between a and n * Match zero or more occurrences of previous character
in expression \ Escape meaning of next character(e.g. \$ lets you
search for $) \\ Escape the \ character
SAVE TEXT AND EXIT VI
ZZ or :wq or
:x Save file and exit vi :w file Save file but do not exit. Omitting file saves current file
:w! file Save file overriding normal checking :n,mw file Write lines n through m to file :n,mw>>file Append lines n through m to end of file
:q Leave vi, saving changes before last write (you may be prompted to save first)
:q! Leave vi without saving any changes since last write Q Escape vi into ex editor with same file; :vi returns
:e! File-edit current file, disregarding changes since last write.
SEARCHING
f<char> Search forward in current line to <char> F<char> Search backwards in curretn line to <char>
/<string> Find next occurence of <string> ?<string> Find previous occurence of <string> :set ic Ingnore case while searching
:set noic Case sensitive while searching (default)
GLOBAL SEARCH AND REPLACE
:n,m s/<str1>/<str2>/<opt> Search from n to m for <str1> and replace
it with <str2> with option <opt>.
<opt> can be c - confirm changes
g - global replace
p - to print changed lines
(Ex- :1,$ s/foo/bar/g Replaces every occurence of "foo" with the
string "bar".)
:g/<str>/<cmd> Run <cmd> on all lines that contain <str>
:g/<str1>/s/<str2>/<str3>/ Find line containing <str1>, replace <str2>
with <str3>
:v/<str>/<cmd> Execute <cmd> on all lines that do not
match <str>
COPYING AND PLACING TEXT
( These commands may be preceded by a number to multiply commands ) yy or Y Yank current line into the buffer ( copy ) nyy or nY Yank n amount of line into buffer
p Insert line(s) in buffer after cursor; also prints last deleted text
P Insert line(s) in buffer before cursor
USEFUL OPTIONS
:set all Displays all options you may "set"
:set showmode Displays "INPUT MODE" while in input mode :set tabstop=n Sets the tabwidth of the tab key to n spaces;
8 is default, but 4 may be preferable :set list Displays all control characters in document;
end of lines are shown as '$'
|