CheatSheet for [VI/VIM] Necessary Commands
I listed all the necessary commands that I used in my daily terminal work life.
What is a VI/VIM?
Vi/VIM - A Text Editor For Unix based Systems like Linux, macOS, and FreeBSD.
How to open a file Using Vim?
vim File_name
It open a created file, if the file is missing then it creates a new file.
Vim - has three modes.
Insertion Mode
Execution Mode
Visual Mode
After opening the editor will open up the execution mode. To insert a text you have to switch the mode.
To get into the Insertion mode - Press i button
To return Back to the Execution mode - Press esc button
Navigation in a file
Key | Work |
h | Move the cursor left. |
j | Move the cursor down. |
k | Move the cursor up. |
l | Move the cursor right. |
gg | Move the cursor to the beginning of the file. |
G | Move the cursor to the end of the file. |
:n | Go to line number 'n'. |
Editing
Key | Work |
i | Enter insert mode at the current cursor position. |
a | Enter insert mode after the current cursor position. |
o | Open a new line below the current line and enter insert mode. |
O | Open a new line above the current line and enter insert mode. |
x | Delete the character selected by the cursor. |
dd | Delete the current line. |
yy | copy the current line. |
p | Paste the previously copied or deleted text. |
Undo and Redo
Key | Works |
u | Undo the last change. |
U | Undo all changes on the current line. |
Ctrl+r | Redo the last change. |
Search and Replace
Key | Works |
/pattern | Search through the document for specified text. |
?pattern | Search backwards through document for specified text. |
n | Move to the next occurrence of the search pattern. |
N | Move to the previous occurrence of the search pattern. |
Saving and Exit
After pressing the esc button when you enter the colon (:) operator, you will see a small command prompt section appears at the bottom-left of the editor.There you can put below commands to do different tasks.
Command | Work |
:w {file name} | Saves file with the specified name. |
:q | Quits when no changes have been made after the last save. |
:wq / x | Saves the current file and quits. |
:q! | Quits, ignoring changes made. |
:e! | Reverts to the last saved format without closing the file. |
:qa | Quits multiple files. |
Who uses VI/VIM ?
Vim is a powerful text editor that can be used for a wide range of tasks. It is especially popular among programmers and system administrators.
Best resource to practice VI/VIM
Type
vimtutor
on a Unix-based terminal
Subscribe to my newsletter
Read articles from S M Mahmud Hasan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
S M Mahmud Hasan
S M Mahmud Hasan
A person who is extremely motivated to acquire and explore new technical information and to share that expertise with others.