Skip to content

Vim

Vim Command Guide

This guide covers the most useful Vim commands for real day-to-day work.

1. Vim Modes

ModeWhat it doesEnterExit
NormalNavigate, edit, run commandsEscN/A
InsertType texti, a, o, I, A, OEsc
VisualSelect textv, V, Ctrl-vEsc
Command-lineRun : commands, search:, /, ?Enter or Esc

2. Open, Save, Quit

CommandAction
vim file.txtOpen file
:e file.txtOpen another file
:wSave
:w newname.txtSave as
:qQuit
:q!Quit without saving
:wqSave and quit
:xSave and quit only if changed
ZZSave and quit (Normal mode)
:waSave all open files
:qaQuit all
:qa!Force quit all

3. Movement (Core Productivity)

Basic movement

CommandAction
h j k lLeft, down, up, right
w / WNext word start
b / BPrevious word start
e / EEnd of word
0Start of line
^First non-blank in line
$End of line
ggStart of file
GEnd of file
42GGo to line 42
%Jump to matching ()[]{}

Fast character and line jumps

CommandAction
fXFind character X forward
FXFind character X backward
tXMove before X
TXMove after X backward
;Repeat last f/F/t/T
,Repeat last f/F/t/T opposite direction

Screen movement

CommandAction
Ctrl-d / Ctrl-uHalf-page down/up
Ctrl-f / Ctrl-bFull-page down/up
zzCenter current line
ztCurrent line to top
zbCurrent line to bottom
H / M / LTop/middle/bottom visible line

4. Insert and Replace

CommandAction
iInsert before cursor
aInsert after cursor
IInsert at first non-blank
AInsert at end of line
oNew line below
ONew line above
rXReplace one character with X
RReplace mode (overwrite until Esc)

5. Editing Operators (Most Important Concept)

Pattern: operator + motion Example: dw = delete to next word.

Operators

CommandAction
dDelete
cChange (delete then insert)
yYank (copy)
gUUppercase
guLowercase
>Indent right
<Indent left
=Auto-indent

High-value combos

CommandAction
ddDelete line
ccChange entire line
yyYank line
DDelete to end of line
CChange to end of line
xDelete character under cursor
XDelete character before cursor
sSubstitute char and enter Insert
JJoin line below with space
gJJoin without adding space

6. Text Objects (Essential)

Pattern: operator + text object Examples: ciw, di(, ya".

CommandAction
iw / awInner word / around word
iW / aWInner WORD / around WORD
i" / a"Inside quotes / around quotes
i' / a'Inside single quotes / around
i( / a(Inside parentheses / around
i[ / a[Inside brackets / around
i{ / a{Inside braces / around
it / atInside tag / around tag
ip / apInner paragraph / around paragraph

7. Visual Mode

CommandAction
vCharacter-wise selection
VLine-wise selection
Ctrl-vBlock (column) selection
oSwitch selection end
gvReselect previous visual selection
d / c / yDelete/change/yank selection
> / <Indent selection
gU / guUpper/lowercase selection

8. Copy, Paste, Registers, Clipboard

CommandAction
p / PPaste after / before cursor
"ayyYank line into register a
"apPaste register a
:regShow registers
"+yYank to system clipboard
"+pPaste from system clipboard

9. Undo, Redo, Repeat

CommandAction
uUndo
Ctrl-rRedo
.Repeat last change
5.Repeat last change 5 times
g- / g+Move older/newer in undo tree

10. Search and Replace

Search navigation

CommandAction
/patternSearch forward
?patternSearch backward
n / NNext / previous match
* / #Search current word forward/backward
:nohlsearch or :nohClear highlighted matches

Substitute

CommandAction
:s/old/new/Replace first match in current line
:s/old/new/gReplace all matches in current line
:%s/old/new/gReplace all matches in file
:%s/old/new/gcReplace all with confirm
:'<,'>s/old/new/gReplace in visual selection

Useful search settings

CommandAction
:set hlsearchHighlight matches
:set incsearchShow match while typing
:set ignorecaseCase-insensitive by default
:set smartcaseCase-sensitive when uppercase used

11. Buffers, Windows, Tabs

Buffers (open files in memory)

CommandAction
:ls or :buffersList buffers
:bnext / :bprevNext/previous buffer
:b 3Switch to buffer 3
:bdDelete current buffer
:e!Reload file, discard unsaved edits

Windows (splits)

CommandAction
:splitHorizontal split
:vsplitVertical split
Ctrl-w h/j/k/lMove between splits
Ctrl-w wCycle splits
Ctrl-w qClose split
Ctrl-w oKeep only current split
Ctrl-w =Equalize split sizes
Ctrl-w + / Ctrl-w -Resize height
Ctrl-w > / Ctrl-w <Resize width

Tabs

CommandAction
:tabnewNew tab
:tabedit file.txtOpen file in new tab
gt / gTNext/previous tab
:tabn / :tabpNext/previous tab
:tabcloseClose tab
:tabonlyKeep only current tab

12. Marks, Jumps, Change Navigation

CommandAction
maSet mark a
`aJump to exact mark position
'aJump to mark line
:marksList marks
Ctrl-o / Ctrl-iBack/forward in jump list
g; / g,Older/newer in change list

13. Macros (Automation)

CommandAction
qaStart recording macro in register a
qStop recording
@aRun macro in register a
@@Re-run last macro
10@aRun macro 10 times

14. Indentation and Formatting

CommandAction
>> / <<Indent/unindent line
>GIndent from cursor to end of file
=%Re-indent matching block
gg=GRe-indent whole file

15. Folding

CommandAction
zaToggle fold
zo / zcOpen/close fold
zR / zMOpen all / close all folds
zf{motion}Create fold over motion
zdDelete one fold

16. Command-line Ranges and Global Edits

CommandAction
:1,10dDelete lines 1-10
:%dDelete all lines
:g/pattern/dDelete lines matching pattern
:v/pattern/dDelete lines not matching pattern
:%!sortFilter whole file through sort command

17. Line Endings and ^M

^M means carriage return (\r) is present.

CommandAction
:set ff?Show file format (unix or dos)
:%s/\r$//Remove trailing \r on all lines
:set ff=unixSave with LF endings
:set ff=dosSave with CRLF endings
:set listShow invisible characters
:set nolistHide invisible characters

18. Built-in Help

CommandAction
:helpOpen help
:h {topic}Help for topic
:h :sHelp for substitute command
:h wHelp for w motion
:h text-objectsHelp for text objects
Ctrl-]Jump to help tag under cursor
Ctrl-oJump back from help

19. Minimal Starter Vim Config

Use _vimrc on Windows, ~/.vimrc on Linux/macOS.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
set number
set relativenumber
set hlsearch
set incsearch
set ignorecase
set smartcase
set expandtab
set shiftwidth=2
set tabstop=2
set autoindent
set clipboard=unnamedplus
set undofile

## 20. High-ROI Commands to Memorize First

1. i, a, o, Esc
2. w, b, 0, $, gg, G
3. dd, yy, p, u, Ctrl-r
4. ciw, di(, yi"
5. /pattern, n, :%s/old/new/gc
6. :w, :q, :wq, :q!
7. :vsplit, Ctrl-w h/j/k/l
8. qa, @a, .