Ultimate Vim cheat sheet
Vim, short for “Vi Improved,” is a powerful and highly configurable text editor that has been a favorite among developers and system administrators for decades. Known for its efficiency and versatility, Vim is a modal text editor that allows users to navigate, edit, and manipulate text with lightning speed once they’ve mastered its unique keyboard-driven interface. Learning Vim can be a hard task for beginners, but the rewards are well worth the effort.
Basic navigation
Vim provides a robust set of commands for navigating text. These commands allow you to move around your document quickly and efficiently, saving valuable time during coding or writing tasks. By combining them, you can quickly traverse your text, locate specific sections, and move precisely where you need to be.
h
: Move the cursor left.j
: Move the cursor down.k
: Move the cursor up.l
: Move the cursor right.w
: Move forward by a word.b
: Move backward by a word.0
: Move to the beginning of the current line.$
: Move to the end of the current line.gg
: Move to the beginning of the document.G
: Move to the end of the document.{
: Move to the beginning of the current paragraph.}
: Move to the end of the current paragraph.Ctrl + u
: Move half a page up.Ctrl + d
: Move half a page down.
Insert and edit text
Once you’ve learned Vim’s navigation, the next step is efficient text insertion and editing. Vim offers a wide array of commands for inserting, deleting, and replacing text. These commands empower you to make changes swiftly without needing to lift your hands from the keyboard.
i
: Insert text before the cursor.I
: Insert text at the beginning of the current line.a
: Append text after the cursor.A
: Append text at the end of the current line.o
: Open a new line below the current line.O
: Open a new line above the current line.r
: Replace the character under the cursor.x
: Delete the character under the cursor.dd
: Delete the current line.yy
: Yank (copy) the current line.p
: Paste the last yanked or deleted text.u
: Undo the last action.Ctrl + r
: Redo the last undone action.
Visual mode
Vim’s visual mode allows you to choose characters, lines, or even rectangular blocks of text with ease. Visual mode enhances your ability to manipulate text precisely, making it easier to copy, delete, or modify sections of your document.
v
: Enter visual mode to select text character by character.V
: Enter visual mode to select text line by line.Ctrl + v
: Enter visual block mode to select text in a rectangular block.y
: Yank (copy) the selected text.d
: Delete the selected text.:s/old/new/g
: Substitute “old” with “new” globally in the selected text.
Save and quit
Managing files within Vim is crucial, as it allows you to seamlessly switch between multiple documents and save your work.
:w
: Save the current file.:wq
or:x
: Save and quit.:q
: Quit (only works if no changes were made).:q!
: Quit without saving (forceful exit).:e filename
: Open a new file named “filename.”
Search and replace
With these commands, you can quickly locate specific content and perform global replacements, saving you time and effort during text editing
/searchterm
: Search forward for “searchterm.”?searchterm
: Search backward for “searchterm.”n
: Jump to the next occurrence of the search term.N
: Jump to the previous occurrence of the search term.:%s/old/new/g
: Replace “old” with “new” throughout the entire document.
Advanced commands
Vim’s true power shines through its advanced commands and customization options. While the basics are essential, delving into advanced features can take your Vim proficiency to the next level.
:set number
: Show line numbers.:set nonumber
: Hide line numbers.:set syntax=python
: Set the syntax highlighting to Python.:e!
: Reload the current file from disk (discard unsaved changes).:sp
: Split the current window horizontally.:vsp
: Split the current window vertically.Ctrl + ww
: Switch between split windows.:tabnew
: Open a new tab.gt
: Move to the next tab.gT
: Move to the previous tab.
Customization
Vim is highly customizable. You can create and use your own custom key mappings, define functions, and install plugins to enhance its functionality. To get started with customization, explore your .vimrc
file.