Nothing Special   »   [go: up one dir, main page]

Vi Cheat Sheet: Modes B e E 0 $ 1G G NG:N FC FC H M L %

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

Vi Cheat Sheet 1

Modes Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the return key. Quitting :x :q ZZ :q! Exit, saving changes Exit as long as there have been no Exit and save changes if any have Exit and ignore any changes B e E word ( ) { } 0 $ 1G G nG :n fc Fc H M L % Move a sentence back Move a sentence forward Move a paragraph back Move a paragraph forward

Page

Move to the beginning of blank delimited word Move to the end of the word Move to the end of Blank delimited

Move to the beginning 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 bottom of screen Move to associated ( ), { }, [ ]

changes been made

Inserting Text i I a A o O r R Insert before cursor 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 x Motion h j k l w W b 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 Like deletion, almost all yank commands are performed by typing y followed by a Yanking Text 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 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:

Vi Cheat Sheet 2
motion. For example, y$ yanks to the end of the line. Two other yank commands are: yy :y Yank the current line Yank the current line Search for strings /stringSearch forward for string ?string Search back for string n 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 example cw changes a word. A few other change commands are: C cc Change to the end of the line Change the whole line :s/pattern/string/flags flags. g p P 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 `c 'c Set marker c on this line Go to beginning of marker c line. Go to first non-blank character of marker c line. Put after the position or after the line Put before the position or before the c & N

Page

Search for next instance of string Search for previous instance of string

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). Replace pattern

with string according to Putting text pattern Flag - Confirm replaces. Repeat last :s command Flag - Replace all occurrences of

Regular Expressions . (dot) Any single character except newline * [...] set [^...] the set ^ $ \< \> Anchor - beginning of the line Anchor - end of line Anchor - beginning of word Anchor - end of word Any single character not specified in Zero or more occurrences of any Any single character specified in the character

\(...\) Grouping - usually used to group conditions \n Contents of nth grouping [...] - Set Examples [A-Z] The SET from Capital A to Capital Z

Vi Cheat Sheet 3
[a-z] The SET from lowercase a to lowercase z [0-9] The SET from 0 to 9 (All numerals) [./=+] The SET containing . (dot), / (slash), =, and + [-A-F] The SET from Capital A to Capital F and the dash (dashes must be specified first) [0-9 A-Z] The SET containing all capital letters and digits and a space [A-Z][a-zA-Z] In the first position, the Ranges SET from Capital A to Capital Z. In the second character position, the SET containing all letters Regular Expression Examples /Hello/ /^TEST$/ Matches if the line contains the value Hello Matches if the line contains TEST by itself /^[a-zA-Z]/ Matches if the line starts with any letter /^[a-z].*/ Matches if the first character of the line is a-z and there is at least one more of any character following it /2134$/ 2134 /\(21|35\)/ Matches is the line contains 21 or 35 Note the use of ( ) with the pipe symbol to specify the 'or' condition /[0-9]*/ /^[^#]/ Matches if there are zero or more numbers in the line Matches if the first character is not a # in the line Notes: 1. Regular expressions are case sensitive 2. Regular expressions are to be used where pattern is specified ~ J Toggle up and lower case Join lines Other :w file Write to file :r file Read file in after line :n :p Go to next file Go to previous file Files Matches if line ends with :n,m :. :$ :'c :% Range - Lines n-m Range - Current line Range - Last line Range - Marker c Range - All lines in file Counts

Page

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 occurrence of the letter e. Even insertions may be repeated conveniently with this method, say to insert the same line 100 times.

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.

:g/pattern/ pattern

Range - All lines that contain

:e file Edit file !!program Replace line with output from program

Vi Cheat Sheet 4
. u U Repeat last text-changing command Undo last change Undo all changes to line

Page

You might also like