VI Keyboard Shortcuts – Cheat Sheet

VI

If you are familiar with Linux shell, I’m sure that you have used “vi” for editing your documents but the tool is very difficult, if you are beginner in Linux.

I want to share some keyboard shortcuts that you can use for edit your files faster.

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
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters

Motion

h Move left
j 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 delimited 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 beginning 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 bottom of screen
% Move to associated ( ), { }, [ ]

Deleting Text

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

Yanking Text

yy Yank the current line
:y Yank the current line

Changing text

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 position or before the line

Markers

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.

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

:s/pattern/string/flags Replace pattern with string according to flags.
g Flag – Replace all occurrences of pattern
c Flag – Confirm replaces.
& Repeat last :s command

Regular Expressions

. (dot) Any single character except newline
* zero or more occurrences 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 – beginning of word
\> Anchor – end of word
\(…\) Grouping – usually used to group conditions
\n Contents of nth grouping
[…] – Set Examples
The SET from Capital A to Capital Z
The SET from lowercase a to lowercase z
The SET from 0 to 9 (All numerals)
The SET containing. (dot), / (slash), =, and +
The SET from Capital A to Capital F and the dash (dashes must be specified first)
The SET containing all capital letters and digits and a space
In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters
Regular Expression Examples
Matches if the line contains the value Hello
Matches if the line contains TEST by itself
Matches if the line starts with any letter
Matches if the first character of the line is a-z and there is at least one more of any character following it
Matches if line ends with 2134
Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the ‘or’ condition
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

Ranges

:n,m Range – Lines n-m
:. Range – Current line
:$ Range – Last line
:’c Range – Marker c
:% Range – All lines in file
:g/pattern/ Range – All lines that contain pattern

Files

:w file Write to file
:r file Read file in after line
:n Go to next file
:p Go to previous file
:e file Edit file
!!program Replace line with output from program

Other

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

Using Named Buffers

To repeatedly insert a group of lines in various places within a document, you can yank (or delete) the lines into a named buffer. You specify named buffers by preceding a command with double quotes (") and a name for the buffer. For example, to yank four lines into the named buffer a, type “a4yy“. You can use several different buffers. For example, you might also delete text from one location and add it to several others. To delete 12 lines into the named buffer b, type “b12dd“.

To insert the text, precede the p or P command with n, where n is the named buffer. For example, to insert the lines saved in buffer b, type “bP“.

You can overwrite named buffers with new lines. The buffers are saved until you exit vi.

When you use named buffers, you can safely delete and yank other text without affecting the lines you have already saved in the named buffers — unless, of course, you purposely overwrite the named buffer.

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

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *