In this tutorial, I am going to share some of the most important VI Keyboard Shortcuts, that make your command line tasks super easier. I also included VI Cheatsheet PDF to download.
VI Keyboard Shortcuts: The most practical and commonly used terminal-based editor is VI. As a visual editor, the vi editor has been enhanced and in 2022 Vi Shortcuts and Vi Commands are so much useful to work in VI using the terminal. Every Unix system has it.
In other words, it is accessible in all Linux distributions. It is simple to use and runs on a variety of operating systems and platforms. It is a highly strong program. But now Vim is the enhanced version of the vi editor.
Modes of VI Editor
VI Editor has three modes
Command Mode: When the vi editor starts up in the terminal, it is in Command Mode as default. In command mode Vi interprets any characters we type as commands and thus does not display them in the terminal. This mode allows us to move through a file, and delete, copy, or paste a piece of text. To enter into this Mode from any other mode you have to press the [Esc] key. If you press [Esc] when you are in Command Mode, then vi will beep or flash the screen and change to that required mode using vi keyboard commands and vi shortcuts.
Insert Mode: This mode allows you to input text into the terminal. Everything written in this mode is evaluated as input and thereafter saved to a file. The vi always opens in command mode as default. The Insert mode is required to enter text. Simply type I to enter insert mode. To exit insert mode, use the Esc key, which returns you to command mode.
Last Line Mode: While in Command Mode, type a colon [:] to enter Line Mode. The cursor will go to the final line of the terminal while vi waits for a command. This mode allows you to do things like save files and run commands.
Vi Commands and their Description
Vi Editor and the vi shortcut and vi commands are different from other editors and they used different functions to execute.
The commands used in the vi editor are case-sensitive.
Command
Description
vi filename
Creates a new file if it does not exist, otherwise opens the existing file.
vi -R filename
Opens an existing file in read-only mode. view filename: Opens an existing file in read-only mode.
To switch from command to insert mode
Command
Action
i
Start typing before the current character
I
Start typing at the start of the current line
a
Start typing after the current character
A
Start typing at the end of the current line
o
Start typing on a new line after the current line
O
Start typing on a new line before the current line
To save and quit (VI Keyboard Shortcuts)
Commands
Action
:wq
Save and quit
:w
Save
:q
Quit
:w fname
Save as fname
ZZ
Save and quit
:q!
Quit discarding changes made
:w!
Save (and write to the non-writable file)
To move around a file VI text editor
Commands
Action
j
To move down
k
To move up
h
To move left
l
To move right
To jump lines on VI text editor
Commands
Action
G
Will direct you at the last line of the file
“
Will direct you to your last position in the file
To delete on VI text editor
Commands
Action
x
Delete the current character
X
Delete the character before the cursor
r
Replace the current character
xp
Switch two characters
dd
Delete the current line
D
Delete the current line from the current character to the end of the line
dG
delete from the current line to the end of the file
To repeat and undo (VI Keyboard Shortcuts)
Commands
Action
u
Undo the last command
.
Repeat the last command.
Command to cut, copy and paste
Commands
Action
dd
Delete a line
yy
(yank yank) copy a line
p
Paste after the current line
P
Paste before the current line
Command to cut, copy and paste in blocks
Commands
Action
<n>dd
Delete the specified n number of lines
<n>yy
Copy the specified n number of lines
Start and end of the line VI text editor
Commands
Action
θ
Bring at the start of the current line
^
Bring at the start of the current line
$
Bring at the end of the current line
dθ
Delete till the start of a line
d$
Delete till the end of a line
Joining lines in VI text editor
Commands
Action
J
Join two lines
yyp
Repeat the current line
ddp
Swap two lines
Move forward or backward in VI
Commands
Action
w
Move one word forward
b
Move one word backward
<n>w
Move specified number of words forward
dw
Delete one word
yw
Copy one word
<n>dw
Delete specified number of words
Search a string in VI text editor
Commands
Action
/string
Forward search for given string
?string
Backward search for given string
/^string
Forward search string at beginning of a line
/string$
Forward search string at end of a line
n
Go to the next occurrence of the searched string
/\<he\>
Search for the word he (and not for there, here, etc.)
In this article, I covered most of the important and most used VI keyboard Shortcuts with the VI commands cheatsheet in PDF. Download the PDF and also share it with your friends.
Sulagna is a tech enthusiastic girl, she is exploring the different fields of technology and sharing her learning through her writings with everyone . You can follow her at YouTube(Noobie Techs)