Vi / Vim simplified - CheatSheet

In this article I'm going to share my most used commands in Vim that help me in day to day terminal workloads

Vi - Unix Editor, Other editors are like Emacs and Nano.

Syntax to open a file using Vim.

vim File_name

The above command opens an already-created file, else if the file is missing it creates a new file.

Vim - has two operating modes.

  • Insertion mode

  • Execution mode

It starts with the Execution mode, to make changes to the File - you have to be in the Insertion mode. At any time, you can see which mode you are in on the status bar which is located at the top of the editor.

To get into Insertion mode - press i

ModeEnables Users To
InsertInsert text by typing.
ExecuteExecute commands within the editor.
EscReturns to command mode.

Execute Modes Commands

Saving and Exiting Vim

To go back to the Execute command, when you enter the colon (:) operator, a small command prompt section appears at the bottom-left of the editor. That is where you watch for the commands to exit the work.

CommandFunction
:w {file name}Saves file with the specified name.
:qQuits when no changes have been made after the last save.
:q!Quits, ignoring changes made.
:qaQuits multiple files.
:wqSaves the current file and quits.
:e!Reverts to the last saved format without closing the file.
:helpOpens Vim’s built-in help documentation.

Navigate through a file in Vim

Navigation KeyUsed to
hMove left one character
jMove down one line
kMove up one line
lMove right one character
Shift+LMove the cursor to the bottom of the screen.
Shift+HMove the cursor to the first line of the screen.

Editing a file in Vim

Editing operators in command mode are powerful tools that can be used to manipulate text with simple keystrokes.

NB: They can also be used in combination with motions to edit multiple characters.

CommandAction
aInsert characters to the right of the cursor
AAppend characters to the current line
iInsert characters to the left of the cursor
IInsert characters at the beginning of the current line
oAdd a new line after the current line
OInsert a new line above the current line

Deleting characters/ words in the file

Command

Action

x

Delete the character selected by the cursor.

d

Delete text.

dd

Delete the current line.

Copying and Pasting on the file

Command

Action

p

Paste text on the line below the cursor.

P

Paste text on the line above the cursor.

y

Copy text.

yy

Copy the line above the cursor.

Undo option

Command

Action

u

Undo the latest change.

U

Undo all changes on the current line.

Searching through the file

Command

Action

/{text string}

Search through the document for specified text.

?{text string}

Search backwards through document for specified text.

Best Resources to Practise

Blogs

Games or Interactive Sessions

  1. OpenVim

  2. Type vimtutor on a Unix-based terminal

  3. VimAdventures