Vim (Vi IMproved) is a highly powerful text editor used in Unix-based systems. It is an advanced version of Vi editor, thus offers features like syntax highlighting, multi-level undo, and extensive plug-in support.
However, the tool comes with too many commands, which is why a Vim Cheat Sheet is your best friend to navigate through the mess!
Vim Modes Overview:
Mode | Description | Activation |
Normal Mode | Default mode for navigation and text manipulation | Press Esc |
Insert Mode | Allows direct text input, similar to a standard text editor | Press i, a, o, etc. |
Visual Mode | Enables text selection for copying, deleting, or modifying | Press v (character mode), V (line mode), Ctrl + v(block mode) |
Command Mode | Used for executing commands like saving, quitting, and searching | Press : followed by a command |
Replace Mode | Overwrites text instead of inserting | Press R |
Basic Commands
- vim filename → Open a file in Vim
- :q → Quit Vim
- :q! → Quit without saving
- :w → Save the file
- :wq or ZZ → Save and quit
- :help command → Get help for a specific command
Navigation in Vim
To navigate within a file by characters, words, tokens, or lines. You need specific commands to move the cursor.
Moving By The Screens
- Ctrl + b – move back one full screen
- Ctrl + f – move forward one full screen
- Ctrl + d – move forward 1/2 a screen
- Ctrl + u – move back 1/2 a screen
- Ctrl + e – move screen down one line (without moving the cursor)
- Ctrl + y – move screen up one line (without moving the cursor)
- Ctrl + o – move backward through the jump history
- Ctrl + i – move forward through the jump history
- H – move to the top of the screen (H=high)
- M – move to the middle of the screen (M=middle)
- L – move to the bottom of the screen (L=low)
Related Article: Install Vim on Ubuntu in 5 Minutes!
Moving Within A Line
- h → Move left
- l → Move right
- 0 → Move to the beginning of the line
- ^ → Move to the first non-blank character
- $ → Move to the end of the line
Moving Between Words
- w → Move forward one word
- b → Move backward one word
- e → Move to the end of the current/next word
Moving Across The File
- gg → Move to the beginning of the file
- G → Move to the end of the file
- :n → Move to line n (e.g., :10 goes to line 10)
- Ctrl + d → Move half a page down
- Ctrl + u → Move half a page up
Editing and Manipulating Text
Editing Text
- r – replace a single character (and return to command mode)
- cc – replace an entire line (deletes the line and moves into insert mode)
- C / c$ – replace from the cursor to the end of a line
- cw – replace from the cursor to the end of a word
- s – delete a character (and move into insert mode)
- J – merge the line below to the current one with a space in between them
- gJ – merge the line below to the current one with no space in between them
- u – undo
- Ctrl + r – redo
- . – repeat last command
Inserting & Replacing Text
- i → Insert before the cursor
- a → Insert after the cursor
- o → Open a new line below
- O → Open a new line above
- r<char> → Replace a single character
- R → Enter replace mode (overwrite text)
Deleting & Replacing Text
- x → Delete the character under the cursor
- dd → Delete the current line
- D → Delete from the cursor to the end of the line
- dw → Delete a word
- d$ → Delete from the cursor to the end of the line
- dG → Delete from the cursor to the end of the file
Copy & Pasting Text
- yy → Copy (yank) the current line
- y$ → Copy from the cursor to the end of the line
- yw → Copy a word
- p → Paste after the cursor
- P → Paste before the cursor
Search and Replace
Searching
- /word → Search for “word” forward
- ?word → Search for “word” backward
- n → Repeat last search in the same direction
- N → Repeat last search in the opposite direction
Replacing
- :%s/old/new/g → Replace all occurrences of “old” with “new” in the file
- :%s/old/new/gc → Replace with confirmation
Working with Multiple Files
- :e filename → Open another file
- :n → Switch to the next file
- :prev → Switch to the previous file
- :split filename → Split window horizontally and open a file
- :vsplit filename → Split window vertically and open a file
- Ctrl + w → Switch between split windows
Advanced Features
- :set number → Show line numbers
- :set nonumber → Hide line numbers
- :syntax on → Enable syntax highlighting
- :syntax off → Disable syntax highlighting
- :set ignorecase → Set case-insensitive search
- :set noignorecase → Set case-sensitive search
- :set autoindent → Enable auto-indentation
- :set noautoindent → Disable auto-indentation
Conclusion – Vim Cheat Sheet
Vim is an incredibly powerful text editor that can greatly enhance your productivity once you master its commands. By using shortcuts for navigation, editing, and file management, you can edit text efficiently without relying on a mouse. Whether you’re a beginner or an advanced user, keeping this Vim cheat sheet handy will help you streamline your workflow. Happy coding!
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
1. What is the difference between Vim and Vi?
Vim (Vi Improved) is an enhanced version of Vi, offering additional features like syntax highlighting, multi-level undo, and plugin support.
2. What is Vim used for?
Vim is a powerful text editor used for programming, scripting, and general text editing, offering efficiency through keyboard shortcuts.
3. How do I exit Vim?
Use :q
to quit, :q!
to quit without saving, and :wq
or ZZ
to save and exit.