Monday, January 17, 2011

vi command summary

vi command summary

The following tables contain all the basic vi commands.
Starting vi

CommandDescription
vi filestart at line 1 of file
vi +n filestart at line n of file
vi + filestart at last line of file
vi +/pattern filestart at pattern in file
vi -r filerecover file after a system crash

Saving files and quitting vi

CommandDescription
:e fileedit file (save current file with :w first)
:wsave (write out) the file being edited
:w filesave as file
:w! filesave as an existing file
:qquit vi
:wqsave the file and quit vi
:xsave the file if it has changed and quit vi
:q!quit vi without saving changes

Moving the cursor

Keys pressedEffect
hleft one character
l or <Space>right one character
kup one line
j or <Enter>down one line
bleft one word
wright one word
(start of sentence
)end of sentence
{start of paragraph
}end of paragraph
1Gtop of file
nGline n
Gend of file
<Ctrl>Wfirst character of insertion
<Ctrl>Uup ½ screen
<Ctrl>Ddown ½ screen
<Ctrl>Bup one screen
<Ctrl>Fdown one screen

Inserting text

Keys pressedText inserted
aafter the cursor
Aafter last character on the line
ibefore the cursor
Ibefore first character on the line
oopen line below current line
Oopen line above current line

Changing and replacing text

Keys pressedText changed or replaced
cwword
3cwthree words
cccurrent line
5ccfive lines
rcurrent character only
Rcurrent character and those to its right
scurrent character
Scurrent line
~switch between lowercase and uppercase

Deleting text

Keys pressedText deleted
xcharacter under cursor
12x12 characters
Xcharacter to left of cursor
dwword
3dwthree words
d0to beginning of line
d$to end of line
ddcurrent line
5ddfive lines
d{to beginning of paragraph
d}to end of paragraph
:1,. dto beginning of file
:.,$ dto end of file
:1,$ dwhole file

Using markers and buffers

CommandDescription
mfset marker named ``f''
`fgo to marker ``f''
´fgo to start of line containing marker ``f''
"s12yycopy 12 lines into buffer ``s''
"ty}copy text from cursor to end of paragraph into buffer ``t''
"ly1Gcopy text from cursor to top of file into buffer ``l''
"kd`fcut text from cursor up to marker ``f'' into buffer ``k''
"kppaste buffer ``k'' into text

Searching for text

SearchFinds
/andnext occurrence of ``and'', for example, ``and'', ``stand'', ``grand''
?andprevious occurrence of ``and''
/^Thenext line that starts with ``The'', for example, ``The'', ``Then'', ``There''
/^The\>next line that starts with the word ``The''
/end$next line that ends with ``end''
/[bB]oxnext occurrence of ``box'' or ``Box''
nrepeat the most recent search, in the same direction
Nrepeat the most recent search, in the opposite direction

Searching for and replacing text

CommandDescription
:s/pear/peach/greplace all occurrences of ``pear'' with ``peach'' on current line
:/orange/s//lemon/gchange all occurrences of ``orange'' into ``lemon'' on next line containing ``orange''
:.,$/\<file/directory/greplace all words starting with ``file'' by ``directory'' on every line from current line onward, for example, ``filename'' becomes ``directoryname''
:g/one/s//1/greplace every occurrence of ``one'' with 1, for example, ``oneself'' becomes ``1self'', ``someone'' becomes ``some1''

Matching patterns of text

ExpressionMatches
.any single character
*zero or more of the previous expression
.*zero or more arbitrary characters
\<beginning of a word
\>end of a word
\quote a special character
\*the character ``*''
^beginning of a line
$end of a line
[set]one character from a set of characters
[XYZ]one of the characters ``X'', ``Y'', or ``Z''
[[:upper:]][[:lower:]]*one uppercase character followed by any number of lowercase characters
[^set]one character not from a set of characters
[^XYZ[:digit:]]any character except ``X'', ``Y'', ``Z'', or a numeric digit

Options to the :set command

OptionEffect
alllist settings of all options
ignorecaseignore case in searches
listdisplay <Tab> and end-of-line characters
mesgdisplay messages sent to your terminal
nowrapscanprevent searches from wrapping round the end or beginning of a file
numberdisplay line numbers
report=5warn if five or more lines are changed by command
term=ansiset terminal type to ``ansi''
terseshorten error messages
warndisplay ``[No write since last change]'' on shell escape if file has not been saved

No comments:

Post a Comment