Vim Cheatsheet
#
Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor with many enhancements.
Basic Navigation
#
Command/Option | Example | Description |
---|
h | h | Move cursor left |
j | j | Move cursor down |
k | k | Move cursor up |
l | l | Move cursor right |
gg | gg | Move to the beginning of the file |
G | G | Move to the end of the file |
Editing Text
#
Command/Option | Example | Description |
---|
i | i | Enter insert mode before the cursor |
I | I | Enter insert mode at the beginning of the line |
a | a | Enter insert mode after the cursor |
A | A | Enter insert mode at the end of the line |
o | o | Open a new line below the current line |
O | O | Open a new line above the current line |
x | x | Delete the character under the cursor |
dd | dd | Delete the current line |
yy | yy | Yank (copy) the current line |
p | p | Paste the yanked or deleted text |
Search and Replace
#
Command/Option | Example | Description |
---|
/search | /example | Search for “example” |
?search | ?example | Search backward for “example” |
n | n | Move to the next search result |
N | N | Move to the previous search result |
:%s/old/new/g | :%s/old/new/g | Replace all occurrences of “old” with “new” in the file |
:s/old/new/g | :s/old/new/g | Replace all occurrences of “old” with “new” in the current line |
File Operations
#
Command/Option | Example | Description |
---|
:w | :w | Save the current file |
:q | :q | Quit Vim |
:wq | :wq | Save and quit Vim |
:x | :x | Save and quit Vim (alternative to :wq ) |
:q! | :q! | Quit without saving changes |
:e filename | :e newfile.txt | Open a file named “newfile.txt” |
:w filename | :w newfile.txt | Save the current file as “newfile.txt” |
Window Management
#
Command/Option | Example | Description |
---|
:split | :split | Split the window horizontally |
:vsplit | :vsplit | Split the window vertically |
Ctrl-w w | Ctrl-w w | Switch between windows |
Ctrl-w q | Ctrl-w q | Close the current window |
Visual Mode
#
Command/Option | Example | Description |
---|
v | v | Enter visual mode to select characters |
V | V | Enter line-wise visual mode |
Ctrl-v | Ctrl-v | Enter block visual mode |
d | d | Delete the selected text |
y | y | Yank (copy) the selected text |
p | p | Paste the yanked or deleted text |
Advanced Commands
#
Command/Option | Example | Description |
---|
:set number | :set number | Show line numbers |
:set relativenumber | :set relativenumber | Show relative line numbers |
:syntax on | :syntax on | Enable syntax highlighting |
:set tabstop=4 | :set tabstop=4 | Set the width of a tab to 4 spaces |
:set shiftwidth=4 | :set shiftwidth=4 | Set the number of spaces for auto-indents |
This cheatsheet covers the essential Vim commands and options to help you navigate, edit, and manage files efficiently in Vim.