How to edit, save, exit Vi (VIM): A Complete Vi Cheat Sheet for Linux users

(VIM) Vi text editor is meant for the Unix operating system, originally. Most of the Linux system uses the Vi text editor along with embedded systems. It is similar to Windows NotePad but without a user-friendly interface. It is just like Nano text editor, however, the Nano is a bit interactive & easy to operate than the Vi.

You also might be heard about VIM which is an enhanced version based on original Vi. If you are started using Vi text editor to edit some file on Linux OS then you always struggle with the Keyboard shortcuts that needed to operate it. So, just make your editing a bit easy with Vi in Putty or Command terminal; here a list of commands or keyboard shortcuts used in Linux Vi text editor.

VIM VI commands

(VIM) Vi editor cheat sheet to know about how to exit, edit, insert, write, quite, save and more…

Vi editor shortcuts pdf:

Click here to download VI cheat sheet ACNS bulletin

Vi Text editor Mode

The Vi has two Mode one is command mode where the user can only move the cursor to select the text to perform deletion and pasting jobs, the second one insertion which enables when you press the INSERT button from the keyboard to perform insert or change command. The insertion mode allows to edit and insert text from the file. When the insertion is over you can press the ESC button to come back to the command mode of VI. To Exit and save your file in VI just from command mode type :wq and for only quite the or exit the VI just type :q or :quite and press Enter Key/return key.

Commands for VIM-Vi Quit and Save

: xTo Exit Vi and saving changes
:qTo Exit Vi if there have been no changes
ZZExit Vi in case of saving changes if any have been made
:q!Exit and ignore any changes

Vi text deletes Commands

To delete text in Vi you can simply use the Delete button from the keyboard, however, here are some other commands:

xDelete character to the right of cursor
XDelete character to the left of the cursor
DDelete to the end of the line
ddDelete current line
:dDelete current line

 

VI Inserting Text Commands

These are commands to insert text:

iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters

 

Vi Navigation commands

hMove left
jMove down
kMove up
lMove right
wMove to next word
WMove to next blank delimited word
bMove to the beginning of the word
BMove to the beginning of blank delimted word
eMove to the end of the word
EMove to the end of Blank delimited word
(Move a sentence back
)Move a sentence forward
{Move a paragraph back
}Move a paragraph forward
0Move to the begining of the line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
MMove to middle of the screen
LMove to botton of screen
%Move to associated ( ), { }, [ ]

 

Vi Yanking Text commands

The Vi Yank commands are same like delete command but use by typing y followed by arrow keys. Yanking means cut, copy and paste. It used to insert the text again which killed or delete using the Yanl command. It is the usual way in Vi to move or copy text. For example, y$ yanks to the end of the line. Other commands are:

yyYank the current line
:yYank the current line

 

Vi Changing text Commands

This command is another deletion command of the Vi text editor which works in the insert mode.

CChange to the end of the line
ccChange the whole line

 

Vi Putting text Commands

pPut after the position or after the line
PPut before the poition or before the line

Vi commands to Search for strings

/stringSearch forward for string
?stringSearch back for string
nSearch for next instance of string
NSearch for previous instance of string

 

Vi Replace commands

:s/pattern/string/flagsReplace pattern with string according to flags.
gFlag – Replace all occurences of the pattern
cFlag – Confirm replaces.
&Repeat last :s command

 

Vi Regular Expressions

. (dot)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
nContents of nth grouping

 

[…] – Set Examples

 
[A-Z]The SET from Capital A to Capital Z
[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 SET from Capital A to Capital Z
In the second character position, the SET containing all letters

 

Regular Expression Examples

 
/Hello/Matches if the line contains the value Hello
/^TEST$/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$/Matches if line ends with 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

Ranges

The Rang command in Vi text editor followed by the colon(:). For example, if you want to delete the 4th & 8the line of the file then the command will be :4,8d.

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

 

Files Managment commands

:w fileWrite to file
:r fileRead the file in after the line
:nGo to next file
:pGo to the previous file
:e fileEdit file
!!programReplace line with the output from a program

 

Other Vi text editor commands

~Toggle upp and lower case
JJoin lines
.Repeat last text-changing command
uUndo last change
UUndo all changes to the line

 

One Response

  1. 88.vn October 1, 2018

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.