Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Vim

Vim


🚀 Opening & Exiting
CommandDescription
vim file.txtOpen file
vim +N file.txtOpen file at line N
vim +/pattern file.txtOpen file at first match of pattern
vim -R file.txtOpen file in read-only mode
:qQuit (fails if unsaved changes)
:q!Force quit, discard changes
:wSave file
:wq or ZZSave and quit
:w filenameSave as new filename
:xSave only if changed, then quit
🎮 Modes
ModeEnter withDescription
NormalEscDefault mode — navigate and run commands
InsertiType and edit text
VisualvSelect characters
Visual LineVSelect whole lines
Visual BlockCtrl+vSelect a block/column
Command-line:Run ex commands
ReplaceROverwrite characters
🧭 Navigation (Normal Mode)

Basic movement

KeyMove
hLeft
jDown
kUp
lRight
wNext word start
bPrevious word start
eNext word end
W / B / ESame but WORD (whitespace-delimited)

Line navigation

KeyMove
0Start of line
^First non-blank character
$End of line
ggFirst line of file
GLast line of file
:N or NGGo to line N

Screen navigation

KeyMove
Ctrl+fPage down
Ctrl+bPage up
Ctrl+dHalf page down
Ctrl+uHalf page up
HTop of screen
MMiddle of screen
LBottom of screen
zzCenter cursor on screen
✏️ Inserting & Editing

Entering insert mode

KeyDescription
iInsert before cursor
IInsert at beginning of line
aAppend after cursor
AAppend at end of line
oOpen new line below
OOpen new line above
sDelete character and insert
SDelete line and insert
CDelete to end of line and insert

Editing in normal mode

KeyDescription
xDelete character under cursor
XDelete character before cursor
ddDelete (cut) current line
DDelete to end of line
dwDelete word
d$Delete to end of line
d0Delete to start of line
yyYank (copy) current line
ywYank word
pPaste after cursor
PPaste before cursor
rReplace single character
~Toggle case of character
uUndo
Ctrl+rRedo
.Repeat last change
🔍 Search & Replace

Searching

CommandDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
nNext match
NPrevious match
*Search for word under cursor (forward)
#Search for word under cursor (backward)
:nohClear search highlighting

Substitution

CommandDescription
:s/old/new/Replace first occurrence on current line
:s/old/new/gReplace all on current line
:%s/old/new/gReplace all in file
:%s/old/new/gcReplace all with confirmation
:5,10s/old/new/gReplace in lines 5–10
📋 Visual Mode & Selection
KeyDescription
vStart character selection
VStart line selection
Ctrl+vStart block selection
oMove to other end of selection
dDelete selected
yYank selected
cChange selected (delete + insert)
>Indent selected
<Unindent selected
=Auto-indent selected
~Toggle case of selected
u / ULowercase / Uppercase selected
🪟 Windows & Tabs

Splits

CommandDescription
:sp or Ctrl+w sHorizontal split
:vsp or Ctrl+w vVertical split
Ctrl+w h/j/k/lMove between splits
Ctrl+w =Equal size splits
Ctrl+w _Maximize height
Ctrl+w |Maximize width
:closeClose current split

Tabs

CommandDescription
:tabnew fileOpen file in new tab
gtNext tab
gTPrevious tab
:tabcloseClose tab
:tabsList all tabs
📁 Buffers & Files
CommandDescription
:e fileEdit a file
:ls or :buffersList open buffers
:b NSwitch to buffer N
:bnNext buffer
:bpPrevious buffer
:bdDelete (close) buffer
:r fileRead file contents into current buffer
:r !cmdRead command output into buffer
⚙️ Useful Settings (:set)
CommandDescription
:set numberShow line numbers
:set relativenumberShow relative line numbers
:set hlsearchHighlight search results
:set incsearchIncremental search
:set ignorecaseCase-insensitive search
:set smartcaseCase-sensitive if uppercase used
:set autoindentAuto-indent new lines
:set tabstop=4Tab width = 4 spaces
:set expandtabUse spaces instead of tabs
:set wrapWrap long lines
:set syntax=onEnable syntax highlighting
:set pastePaste mode (disables auto-indent)

Sample ~/.vimrc

set number
set relativenumber
set hlsearch
set incsearch
set ignorecase
set smartcase
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set wrap
syntax on
🔧 Macros & Registers
CommandDescription
qaStart recording macro into register a
qStop recording
@aPlay macro a
@@Repeat last macro
5@aRun macro a 5 times
"ayYank into register a
"apPaste from register a
:regShow all registers
💡 Tips & Tricks
  • Repeat a command N times: prefix with a number, e.g. 5j moves down 5 lines, 3dd deletes 3 lines

  • Text objects: ciw (change inner word), ci" (change inside quotes), da( (delete around parentheses)

  • Jump list: Ctrl+o (older position), Ctrl+i (newer position)

  • Marks: ma set mark a, `a jump to mark a

  • Sort lines: :%sort (whole file), :5,10sort (lines 5–10)

  • Execute shell command: :!ls

  • Filter lines through command: :%!sort -u

  • Spell check: :set spell, ]s next misspelling, z= suggest corrections