Vim Commands
VIM Commands Cheat Sheet
This page provides a comprehensive guide to using VIM, including navigation, editing, searching, and customization.
1. VIM Modes
VIM operates in different modes:
- Normal Mode - Default mode for navigation and commands (press
Esc
to enter). - Insert Mode - Used for typing text (press
i
,a
,o
). - Visual Mode - Used for selecting text (press
v
orV
). - Command Mode - Used for executing commands (press
:
).
2. Opening and Exiting VIM
vim filename
- Open a file in VIM.:q
- Quit if no changes were made.:q!
- Quit without saving.:w
- Save file.:wq
or:x
- Save and quit.:w filename
- Save as a new file.
Move Cursor
h
- Move left.l
- Move right.j
- Move down.k
- Move up.
Move Faster
Ctrl + u
- Move half-page up.Ctrl + d
- Move half-page down.Ctrl + b
- Move full-page up.Ctrl + f
- Move full-page down.gg
- Go to the beginning of the file.G
- Go to the end of the file.number G
- Go to line number (e.g.,10G
).
4. Editing Text
Insert Mode
i
- Insert before cursor.I
- Insert at the beginning of the line.a
- Append after cursor.A
- Append at the end of the line.o
- Open a new line below.O
- Open a new line above.
Deleting Text
x
- Delete character under cursor.dd
- Delete current line.dw
- Delete word.d$
- Delete from cursor to end of line.d0
- Delete from cursor to beginning of line.D
- Delete from cursor to end of line.:1,10d
- Delete lines 1 to 10.
Copying & Pasting (Yanking)
yy
- Copy (yank) current line.yw
- Copy (yank) word.y$
- Copy (yank) to end of line.p
- Paste after cursor.P
- Paste before cursor.
Undo & Redo
u
- Undo last change.Ctrl + r
- Redo undone change.
5. Searching and Replacing
Search
/word
- Search for "word" forward.?word
- Search for "word" backward.n
- Jump to next match.N
- Jump to previous match.
Replace
:%s/old/new/g
- Replace "old" with "new" globally.:5,10s/old/new/g
- Replace "old" with "new" in lines 5-10.:%s/old/new/gc
- Replace with confirmation.
6. Working with Multiple Files
:e filename
- Open another file.:bn
- Switch to next buffer.:bp
- Switch to previous buffer.:bd
- Close current buffer.
7. Splitting Windows
:split filename
- Split window horizontally and open file.:vsplit filename
- Split window vertically and open file.Ctrl + w + w
- Switch between windows.Ctrl + w + q
- Close current window.
8. Customization
Set Line Numbers
:set number
- Enable line numbers.:set nonumber
- Disable line numbers.
Set Auto Indentation
:set autoindent
- Enable auto indentation.:set noautoindent
- Disable auto indentation.
Set Syntax Highlighting
:syntax on
- Enable syntax highlighting.:syntax off
- Disable syntax highlighting.
Set Tab Width
:set tabstop=4
- Set tab width to 4 spaces.:set expandtab
- Use spaces instead of tabs.:set shiftwidth=4
- Set auto-indent width.
9. Exiting VIM in Different Situations
:q
- Quit if no changes were made.:q!
- Quit without saving.:wq
- Save and quit.:x
- Save and quit (alternative to:wq
).ZZ
- Save and quit (in normal mode).:qa!
- Quit all without saving.
10. Miscellaneous
.
- Repeat last command.J
- Join next line with current line.Ctrl + g
- Show file info.:%!sort
- Sort lines in the file.:r filename
- Insert contents of another file.
Summary
This VIM cheat sheet provides essential commands for efficient text editing. Whether navigating, editing, or searching, mastering these commands will greatly improve productivity.