Sunday, 17 April 2016

vi - cheatsheet



Modes
  • Command mode : The editor begins in command mode, where the cursor movement and text deletion and pasting occur.
    Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.
  • 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!).

Operations

Quitting
:x  Exit, saving changes
:q  Exit as long as there have been no changes
ZZ  Exit and save changes if any have been made
:q!  Exit and ignore any changes

Inserting text
i  Insert before cursor
I  Insert before line
a  Append after cursor
 Append after line
o  Open a new line after current line
 Open a new line before current line
r   Replace one character
R  Replace many characters

Motion
h  Move left
 Move down
k  Move up
l  Move right
w  Move to next word
W  Move to next blank delimited word
b  Move to the beginning of the word
B  Move to the beginning of blank delimted word
e  Move to the end of the word
E  Move to the end of Blank delimited word
 Move a sentence back
 Move a sentence forward
{  Move a paragraph back
}  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
:n  Move to nth line of the file
fc  Move forward to c
Fc  Move back to c
H  Move to top of screen
M  Move to middle of screen
L  Move to botton of screen
%  Move to associated ( ), { }, [ ]

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  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

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  Delete character to the right of cursor
X  Delete character to the left of cursor
D  Delete to the end of the line
dd  Delete current line
:d  Delete current line

Search for strings
/string  Search forward for string
?string  Search back for string
n  Search for next instance of string
N  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).
:s/pattern/string/flags  Replace pattern with string according to flags.
g  Flag - Replace all occurences of pattern
c  Flag - Confirm replaces.
&  Repeat last :s command

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   Range - Lines n-m
:.  Range - Current line
:$  Range - Last line


MISC
:set nu  Displays lines with line numbers on the left side
u  Undo your most recent edit
U  Undo all the edits on a single line

No comments:

Post a Comment

Note: only a member of this blog may post a comment.