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

KeyWork
hMove the cursor left.
jMove the cursor down.
kMove the cursor up.
lMove the cursor right.
ggMove the cursor to the beginning of the file.
GMove the cursor to the end of the file.
:nGo 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

KeyWorks
uUndo the last change.
UUndo all changes on the current line.
Ctrl+rRedo the last change.

Search and Replace

KeyWorks
/patternSearch through the document for specified text.
?patternSearch backwards through document for specified text.
nMove to the next occurrence of the search pattern.
NMove 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.

CommandWork
:w {file name}Saves file with the specified name.
:qQuits when no changes have been made after the last save.
:wq / xSaves the current file and quits.
:q!Quits, ignoring changes made.
:e!Reverts to the last saved format without closing the file.
:qaQuits 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

  1. Type vimtutor on a Unix-based terminal

  2. OpenVim

  3. VimAdventures

0
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.