Vi Cheat Sheet
Vi Cheat Sheet
Vi Cheat Sheet
Modes Quitting Inserting T ext Motion Deleting T ext Yanking Changing T ext Putting T ext Buffers Markers Searching Replacing T ext Regular Expressions Counts Ranges Files Other
Quitting
:x :q ZZ :q! Exit, saving changes Exit as long as there have been no changes Exit and save changes if any have been made Exit and ignore any changes
Inserting Text
i Insert before cursor I a A o O r R Insert before line Append after cursor Append after line Open a new line after current line Open a new line before current line Replace one character Replace many characters
Motion
h j k l w W b B e E ( ) { } 0 $ 1G G nG :n fc Fc H M L Move left Move down Move up Move right Move to next word Move to next blank delimited word Move to the beginning of the word Move to the beginning of blank delimted word Move to the end of the word Move to the end of Blank delimited word Move a sentence back Move a sentence forward Move a paragraph back Move a paragraph forward Move to the begining of the line Move to the end of the line Move to the first line of the file Move to the last line of the file Move to nth line of the file Move to nth line of the file Move forward to c Move back to c Move to top of screen Move to middle of screen Move to botton of screen
% Move to associated ( ), { }, [ ]
Deleting Text
Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are: x X D dd :d Delete character to the right of cursor Delete character to the left of cursor Delete to the end of the line Delete current line Delete current line
Yanking Text
Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are: yy Yank the current line :y Yank the current line
Changing text
The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For wxample cw changes a word. A few other change commands are: C Change to the end of the line cc Change the whole line
Putting text
p Put after the position or after the line P Put before the poition or before the line
Buffers
Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.
Markers
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. mc Set marker c on this line `c Go to beginning of marker c line. 'c Go to first non-blank character of marker c line.
Replace
The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below). :s/pattern/string/flags g c & Replace pattern with string according to flags. Flag - Replace all occurences of pattern Flag - Confirm replaces. Repeat last :s command
Regular Expressions
. (dot) * [...] [^...] ^ $ \< \> \(...\) \n Any single character except newline zero or more occurances of any character Any single character specified in the set Any single character not specified in the set Anchor - beginning of the line Anchor - end of line Anchor - begining of word Anchor - end of word Grouping - usually used to group conditions Contents of nth grouping
Counts
Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with thismethod, say to insert the same line 100 times.
Ranges
Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file. :n,m :. :$ :'c :% :g/pattern/ Range - Lines n-m Range - Current line Range - Last line Range - Marker c Range - All lines in file Range - All lines that contain pattern
Files
:w file :r file :n :p :e file !!program Write to file Read file in after line Go to next file Go to previos file Edit file Replace line with output from program
Other
~ T oggle upp and lower case J Join lines . Repeat last text-changing command u Undo last change U Undo all changes to line