The VI (Visual Editor) and its modern enhanced descendant VIM (Vi IMproved) are the default screen-oriented text editors installed on virtually every Linux, Unix, and POSIX system. Unlike graphical editors (like VS Code or Notepad), VI is a modal editor operated entirely via keyboard commands without relying on a mouse. This cheat sheet organizes all essential VI commands by operational category for beginner BCA students, Linux system administrators, and software developers.
Figure 1: State transitions between Command Mode, Insert Mode, and Last-Line (Ex) Mode in VI/VIM.
When you edit a file in vi, you are actually editing a copy of the file held in a temporary memory buffer rather than the original disk file. The following sections describe commands used when saving a file, quitting vi, or doing both.
The uppercase command ZZ (in command mode) will write changes to the disk file and exit vi immediately, returning you to the shell prompt. Alternatively, you can use the colon (:) Ex mode commands listed below:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
|
Saves (writes) the buffer contents to the file without quitting vi. |
02 |
|
Quits vi if no modifications have been made since the last write. |
03 |
|
Writes changes to the file and exits vi in one step. |
04 |
|
Quick command-mode shortcut to save and exit immediately (no colon needed). |
To discard all edits made to the file during the current session and either start over or quit safely, choose from the following commands:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
|
Discards current modifications and reloads the original file from disk so you can start over. |
02 |
|
Forces vi to exit without saving, discarding all unsaved edits made since opening. |
You must be in Command Mode to move the cursor. If you were typing text, press the Esc key first. While modern arrow keys work in VIM, the classical UNIX touch-typing keys (h, j, k, l) keep your fingers on the home row for maximum editing speed.
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Move cursor one character to the left. |
02 |
| Move cursor down one line. |
03 |
| Move cursor up one line. |
04 |
| Move cursor one character to the right. |
05 |
| Jump forward to the beginning of the next word. |
06 |
| Jump backward to the beginning of the previous word. |
07 |
| Jump forward to the end of the current word. |
08 |
| Move to the very beginning of the current line. |
09 |
| Move to the first non-blank character of the current line. |
10 |
| Move to the end of the current line. |
11 |
| Jump to the first line of the document. |
12 |
| Jump to the last line of the document. |
13 |
| Jump directly to line number n (e.g., |
To reposition the cursor relative to the visible screen terminal without moving the entire file buffer:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| High: Move cursor to the top line of the screen. |
02 |
| Middle: Move cursor to the middle line of the screen. |
03 |
| Low: Move cursor to the bottom line of the screen. |
04 |
| Scroll forward one full screen (downward). |
05 |
| Scroll backward one full screen (upward). |
06 |
| Scroll down half a screen. |
07 |
| Scroll up half a screen. |
To type text into the buffer, transition from Command Mode into Insert Mode using one of the following key triggers:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Insert text immediately before the current cursor position. |
02 |
| Insert text at the beginning of the current line (first non-space character). |
03 |
| Append text immediately after the current cursor position. |
04 |
| Append text at the very end of the current line. |
05 |
| Open a new blank line below the current line and enter insert mode. |
06 |
| Open a new blank line above the current line and enter insert mode. |
07 |
| Replace single character under cursor with the next character typed (stays in command mode). |
08 |
| Enter Overwrite/Replace mode; overwrites characters continuously until Esc is pressed. |
In VI, deleting text automatically cuts it into the unnamed buffer, meaning you can paste it back immediately using the p or P command.
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Delete character directly under the cursor. |
02 |
| Delete character immediately to the left of the cursor. |
03 |
| Delete the entire current line. |
04 |
| Delete 5 consecutive lines starting from the cursor position. |
05 |
| Delete from the current character to the start of the next word. |
06 |
| Delete from the current character to the end of the line. |
07 |
| Delete from the cursor backward to the beginning of the line. |
08 |
| Change word: Deletes word and switches immediately into Insert mode. |
In VI terminology, copying text is called Yank (y) and pasting text is called Put (p). Yanked text is stored in an internal buffer:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Yank (copy) the entire current line into the memory buffer. |
02 |
| Yank (copy) 3 consecutive lines starting from cursor. |
03 |
| Yank from the cursor to the end of the current word. |
04 |
| Yank from cursor to the end of the line. |
05 |
| Paste (put) buffer content after cursor (or on the line below for line yanks). |
06 |
| Paste (put) buffer content before cursor (or on the line above for line yanks). |
Searching and global pattern replacement are among VI's most powerful capabilities, leveraging standard POSIX regular expressions:
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Search forward for the specified pattern or string. |
02 |
| Search backward for the specified pattern or string. |
03 |
| Repeat search in the same direction (next occurrence). |
04 |
| Repeat search in the opposite direction (previous occurrence). |
05 |
| Replace all occurrences of 'old' with 'new' in the current line. |
06 |
| Replace all occurrences of 'old' with 'new' throughout the entire file. |
07 |
| Global replace with interactive confirmation prompt |
08 |
| Enable / disable case-insensitive search matching. |
VIM introduced Visual Mode, allowing interactive text selection just like using a mouse in modern code editors:
v : Enters character-wise Visual mode. Move with arrow keys or h/j/k/l to highlight characters.V : Enters line-wise Visual mode. Highlights full lines. Press d to delete or y to copy highlighted lines.Ctrl + v : Enters Column Block Visual mode. Useful for commenting or indenting multiple lines at once.# 1. Place cursor at the first character of the first line
# 2. Press Ctrl + v to enter Block Visual mode
# 3. Press 'j' down to select the lines you want to comment
# 4. Press Shift + i (capital 'I') to enter Insert mode
# 5. Type '#' (the comment character)
# 6. Press Esc twice -> '#' will appear on all selected lines!
You can customize the VI editing environment during your session by typing : from Command mode:
Setting Command |
Description |
|---|---|
| Displays line numbers alongside the left margin. |
| Hides line numbers from the margin. |
| Enables automatic indentation matching the preceding line. |
| Sets tab space width to 4 columns (ideal for shell and Python scripts). |
| Enables code syntax highlighting in VIM for scripts and C programs. |
Sr.No |
Command |
Descriptions |
|---|---|---|
01 |
| Displays file name, total line count, and cursor position percentage at the bottom status line. |
02 |
| Repeats the last modifying command (e.g., repeat delete or insert). |
03 |
| Undo the most recent editing action. |
04 |
| Redo the last undone change (VIM). |
05 |
| Restore current line to its original state before moving cursor away. |
06 |
| Join the line directly below the cursor onto the end of the current line. |
07 |
| Toggle character case under cursor between lowercase and uppercase. |
08 |
| Temporarily drop to shell to run commands. Type |
09 |
| Execute any Linux command directly from VI (e.g., |
10 |
| Clear and redraw the terminal screen if visual artifacts occur. |
Follow this exact step-by-step exercise in your Linux terminal to test your muscle memory:
vi practice.txtLinux programming with Bash shell. Vi is a modal text editor in Unix. Operating systems rely on system calls.
2G or j. Yank the entire line using yy.G and press p to paste it as a new line.:%s/Unix/Linux/g and hit Enter.:set nu and hit Enter.:wq and hit Enter.cat practice.txt.:wq (or ZZ) and press Enter. This writes the buffer contents to disk and safely exits to the shell.
:q!, and press Enter. The exclamation mark forces vi to abort without recording any modifications.
:%s/old_word/new_word/g. To prompt for interactive confirmation at each match, add c at the end: :%s/old_word/new_word/gc.
dd. To delete 5 consecutive lines, type 5dd.
Ctrl+v), split windows, and script plugins while maintaining 100% backward compatibility with vi commands.
Begin automated programming and explore the 5-part script anatomy in Class 12: Overview of Shell Script.
Continue to Class 12: Overview of Shell Script → Previous: ← Types of Modes | Full Syllabus