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

: x To Exit Vi and saving changes
:q To Exit Vi if there have been no changes
ZZ Exit 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:

x Delete character to the right of cursor
X Delete character to the left of the cursor
D Delete to the end of the line
dd Delete current line
:d Delete current line

 

VI Inserting Text Commands

These are commands to insert 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

 

Vi Navigation commands

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 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 the screen
L Move 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:

yy Yank the current line
:y Yank the current line

 

Vi Changing text Commands

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

C Change to the end of the line
cc Change the whole line

 

Vi Putting text Commands

p Put after the position or after the line
P Put before the poition or before the line

Vi commands to 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

 

Vi Replace commands

:s/pattern/string/flags Replace pattern with string according to flags.
g Flag – Replace all occurences of the pattern
c Flag – 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
\n Contents 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
:’c Range – Marker c
:% Range – All lines in file
:g/pattern/ Range – All lines that contain pattern

 

Files Managment commands

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

 

Other Vi text editor commands

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

 

1 thought on “How to edit, save, exit Vi (VIM): A Complete Vi Cheat Sheet for Linux users”

  1. Unquestionably consider that that you said. Your favorite justification appeared to be on the net the easiest factor to take into account of. I say to you, I definitely get irked
    even as people think about worries that they plainly do not recognize about.
    You controlled to hit the nail upon the top and also defined out the entire thing with
    no need side effect, folks can take a signal.

    Will probably be again to get more. Thanks

    Reply

Leave a Comment

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