Click Here For The Advanced VI Cheatsheet

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Vi Cheat Sheet A Append after line

o Open a new line after current line


O Open a new line before current line

 r Replace one character


Modes  Markers
 Quitting  Searching R Replace many characters
 Inserting Text  Replacing Text
 Motion  Regular
 Deleting Text Expressions
 Yanking  Counts
 Changing Text  Ranges
 Putting Text  Files
 Buffers  Other
Motion

h Move left
Click here for the Advanced VI j Move down
Cheatsheet k Move up
l Move right
w Move to next word
Modes W Move to next blank delimited word
b Move to the beginning of the word
Vi has two modes insertion mode and command B Move to the beginning of blank delimted word
mode. The editor begins in command mode, where e Move to the end of the word
the cursor movement and text deletion and pasting
occur. Insertion mode begins upon entering an E Move to the end of Blank delimited word
insertion or change command. [ESC] returns the ( Move a sentence back
editor to command mode (where you can quit, for
example by typing :q!). Most commands execute ) Move a sentence forward
as soon as you type them except for "colon" { Move a paragraph back
commands which execute when you press the
ruturn key. } Move a paragraph forward
0 Move to the begining of the line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file

Quitting :n Move to nth line of the file


fc Move forward to c
Fc Move back to c
:x Exit, saving changes
H Move to top of screen
:q Exit as long as there have been no changes
M Move to middle of screen
ZZ Exit and save changes if any have been made
L Move to botton of screen
:q! Exit and ignore any changes
% Move to associated ( ), { }, [ ]

Inserting Text
Deleting Text

i Insert before cursor


Almost all deletion commands are performed by
I Insert before line typing d followed by a motion. For example, dw
deletes a word. A few other deletes are:
a Append after cursor
x Delete character to the right of cursor general prefix has the form "c where c is any
lowercase character. for example, "adw deletes a
X Delete character to the left of cursor word into buffer a. It may thereafter be put back
D Delete to the end of the line into text with an appropriate "ap.
dd Delete current line
:d Delete current line

Markers

Yanking Text Named markers may be set on any line in a file.


Any lower case letter may be a marker name.
Markers may also be used as limits for ranges.
Like deletion, almost all yank commands are
mc Set marker c on this line
performed by typing y followed by a motion. For
example, y$ yanks to the end of the line. Two `c Go to beginning of marker c line.
other yank commands are: Go to first non-blank character of
'c
yy Yank the current line marker c line.

:y Yank the current line

Search for strings


Changing text
/string Search forward for string
The change command is a deletion command that ?string Search back for string
leaves the editor in insert mode. It is performed by n Search for next instance of string
typing c followed by a motion. For wxample cw
changes a word. A few other change commands N Search for previous instance of string
are:
C Change to the end of the line
cc Change the whole line

Replace

The search and replace function is accomplished


Putting text with the :s command. It is commonly used in
combination with ranges or the :g command
(below).
p Put after the position or after the line :s/pattern/string/fl Replace pattern with string acc
P Put before the poition or before the line ags ording to flags.
Flag - Replace all occurences of
g
pattern
c Flag - Confirm replaces.
& Repeat last :s command

Buffers

Named buffers may be specified before any


deletion, change, yank or put command. The
Regular Expressions

. (dot) Any single character except newline


* zero or more occurances of any character
[...] Any single character specified in the set Counts
[^...] Any single character not specified in the set
^ Anchor - beginning of the line Nearly every command may be preceded by a
number that specifies how many times it is to be
$ Anchor - end of line performed. For example, 5dw will delete 5 words
\< Anchor - begining of word and 3fe will move the cursor forward to the 3rd
occurence of the letter e. Even insertions may be
\> Anchor - end of word
repeated conveniently with thismethod, say to
\(...\) Grouping - usually used to group conditions insert the same line 100 times.
\n Contents of nth grouping

[...] - Set Examples


[A-Z] The SET from Capital A to Capital Z
The SET from lowercase a to lowercase
[a-z] Ranges
z
[0-9] The SET from 0 to 9 (All numerals)
The SET containing . (dot), / (slash), =, Ranges may precede most "colon" commands and
[./=+]
and + cause them to be executed on a line or lines. For
The SET from Capital A to Capital F and example :3,7d would delete lines 3-7. Ranges are
[-A-F] the dash (dashes must be specified commonly combined with the :s command to
first) perform a replacement on several lines, as with
:.,$s/pattern/string/g to make a replacement from
The SET containing all capital letters the current line to the end of the file.
[0-9 A-Z]
and digits and a space
:n,m Range - Lines n-m
In the first position, the SET from
[A-Z][a- Capital A to Capital Z :. Range - Current line
zA-Z] In the second character position, the :$ Range - Last line
SET containing all letters
:'c Range - Marker c
:% Range - All lines in file
Regular Expression Examples :g/pattern/ Range - All lines that contain pattern
Matches if the line contains the value
/Hello/
Hello
Matches if the line contains TEST by
/^TEST$/
itself
/^[a-zA- Matches if the line starts with any
Z]/ letter
Matches if the first character of the Files
/^[a-z].*/ line is a-z and there is at least one
more of any character following it
/2134$/ Matches if line ends with 2134 :w file Write to file
Matches is the line contains 21 or 35 :r file Read file in after line
/\(21|35\)/ Note the use of ( ) with the pipe
:n Go to next file
symbol to specify the 'or' condition
:p Go to previos file
Matches if there are zero or more
/[0-9]*/
numbers in the line :e file Edit file
Matches if the first character is not a !!program Replace line with output from program
/^[^#]/
# in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used
where pattern is specified

Other
~ Toggle upp and lower case
J Join lines
. Repeat last text-changing command
u Undo last change
U Undo all changes to line

Return to the top


Page produced by Lagmonster - Oct 2000

You might also like