Basic vim commands (cheat sheet)

The vim command invokes the Vim editor. However, the vi command may also be used for this purpose because it automatically redirects the user to Vim. When entered without a file name as an argument, the vim command opens a welcome screen by default. Use the syntax vim {file name} to open a file. If the file does not exist, Vim creates a file by the name specified and opens the file for editing. Vim supports multiple files being opened simultaneously.

Working with multiple windows

You can choose to display multiple files horizontally or vertically. Press Ctrl+W+V to create a vertical split, or press Ctrl+W+S to split the screen horizontally.

Vim Modes

Vim is a modal editor, and its different modes decide the functionality of various keys.

Switching between modes in Vim:

Mode Enables Users To
Insert Insert text by typing.
Execute Execute commands within the editor.
Command Perform different editing actions using single keystrokes.
Visual Highlight or select text for copying, deleting, etc.

Switch Modes

Command mode is the default mode of Vim, but you can switch from command mode to any other mode by using a single keystroke. Some of the keys to switch modes are listed here.

Key Function
i Switches to insert mode and inserts text to left of cursor.
A Switches to insert mode and adds text at end of line.
I Switches to insert mode and inserts text at beginning of line.
o Switches to insert mode and inserts text on new line below cursor.
O Switches to insert mode and inserts text on new line above cursor.
v Switches to visual mode to enable selection, one character at a time.
V Switches to visual mode to enable selection, one line at a time.
: Switches to execute mode to enable users to enter commands.
Esc Returns to command mode.

Execute Mode Commands

In command mode, when you enter the colon (:) operator, a small command prompt section appears at the bottom-left of the editor. This indicates that you are in execute mode and can run commands supported by Vim. Some commands supported by Vim are listed in the following table.

Command Function
:w {file name} Saves file with specified name.
:q Quits when no changes have been made after last save.
:q! Quits, ignoring changes made.
:qa Quits multiple files.
:wq Saves current file and quits.
:e! Reverts to last saved format without closing file.
:!{any Linux command} Executes command and displays results in Vim.
:help Opens Vim’s built-in help documentation.

Motions

Motions are single-key shortcuts that are used to navigate through files in command mode. These keys position the cursor anywhere within a document. They can be used for moving the cursor through characters, words, lines, or even huge blocks of text.

Navigation using the arrow keys

In addition to using the h, j, k, and l keys to navigate through the editor, you can also use the Up, Down, Left, and Right Arrow keys. The conventional navigation keys such as Home, End, Page Up, and Page Down also work in Vim.

Navigation Key Used to
h Move left one character
j Move down one line
k Move up one line
l Move right one character
^ Move to the beginning of the current line
$ Move to the end of the current line.
w Move to the next word.
b Move to the previous word.
e Move to the end of the current word or to the end of the next word.
Shift+L Move the cursor to the bottom of the screen.
Shift+H Move the cursor to the first line of the screen.
(Line #) Shift+G Move the cursor to the specified line number.
gg Move the cursor to the first line of the file.
Shift+G Move the cursor to the last line of the file.

Editing Operators

Editing operators in command mode are powerful tools that can be used to manipulate text with simple keystrokes. They can also be used in combination with motions to edit multiple characters.

Counts

A count is a number that multiplies the effect of keystrokes in Vim. It can be used in combination with motions, operators, or both. When used with a motion, cursor movement is multiplied according to the count specified. When used with editing operators, the action gets repeated the number of times specified.

The syntax for using a count with an operator and a motion is:

operator [count] {motion}
Editing Operator Used To
x Delete character selected by cursor.
d Delete text.
dd Delete current line.
p Paste text on line below cursor.
P Paste text on line above cursor.
/{text string} Search through document for specified text.
?{text string} Search backward through document for specified text.
y Copy text.
yy Copy line above cursor.
c{range of lines}c Begin a change in specified range.
u Undo latest change.
U Undo all changes on current line.
ZZ Write file only if changes were made, then quit Vim.
Related Post