Vim Cheatsheet: Essential Commands in Vim That Will Make Your Coding Experience Seamless
I'll go straight to the point.
We all know how powerful Vim (Vi Improved) is.
It's an efficient, versatile, and a speedy IDE that's used by several top developers.
Below is a summary of common commands used in the Vim text editor.
Navigation
Moving the Cursor:
h
: Move the cursor left.j
: Move the cursor down.k
: Move the cursor up.l
: Move the cursor right.
Word Movement:
w
: Move to the beginning of the next word.b
: Move to the beginning of the previous word.e
: Move to the end of the current word.
Line Movement:
0
(zero): Move to the beginning of the line.^
: Move to the first non-blank character of the line.$
: Move to the end of the line.
Screen Movement:
Ctrl + f
: Move forward one full screen.Ctrl + b
: Move backward one full screen.
Jumping:
gg
: Go to the beginning of the file.G
: Go to the end of the file.<line-number>G
: Go to a specific line.
Editing
Entering Insert Mode:
i
: Insert before the cursor.I
: Insert at the beginning of the line.a
: Insert after the cursor.A
: Insert at the end of the line.o
: Open a new line below the current line.O
: Open a new line above the current line.
Deleting and Changing Text:
x
: Delete the character under the cursor.dd
: Delete the current line.D
: Delete from the cursor position to the end of the line.cw
: Change (replace) the current word.
Copy, Cut, and Paste
Copy and Paste:
yy
: Yank (copy) the current line.yw
: Yank (copy) from the cursor position to the end of the word.p
: Paste after the cursor.P
: Paste before the cursor.
Cut and Paste:
dd
: Cut (delete) the current line.dw
: Cut (delete) from the cursor position to the end of the word.p
: Paste after the cursor.P
: Paste before the cursor.
Undo and Redo
Undo and Redo:
u
: Undo the last change.Ctrl + r
: Redo the last undone change.
Saving and Quitting
Saving:
:w
: Save changes.:w filename
: Save as a new file.
Quitting:
:q
: Quit (exit) if there are no unsaved changes.:q!
: Quit without saving changes.:wq
orZZ
: Save changes and quit.
Searching and Replacing
Search:
/search-term
: Search forward for a term.?search-term
: Search backward for a term.
Search and Replace:
:%s/old/new/g
: Replace all occurrences of "old" with "new" in the entire file.:s/old/new/g
: Replace all occurrences of "old" with "new" in the current line.
These are just a subset of the many commands Vim provides. Vim has a steep learning curve, but mastering it can greatly improve your text editing efficiency.
To learn more, consider exploring built-in Vim tutorials (vimtutor
in the terminal) and the extensive online documentation.
Subscribe to my newsletter
Read articles from Rita Kairu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rita Kairu
Rita Kairu
A coding civil engineer!