Learning Vim


Vim is a modal text editor, which means the same keys do different things depending on the mode you are in. It becomes powerful once you learn its core ideas: motion, text objects, registers, marks, buffers, windows, and substitution.

The main modes

  • Normal mode: the default mode for movement and editing.
  • Insert mode: where you type text.
  • Visual mode: for selecting text.
  • Command-line mode: for commands such as :w, :q, and :s.

A good habit is to press Esc often to return to Normal mode.

Start here

  • Press i to enter Insert mode before the cursor.
  • Press a to enter Insert mode after the cursor.
  • Press o to open a new line below and start typing.
  • Press Esc to return to Normal mode.
  • Press : to enter command-line mode.

Basic motion

  • h left
  • j down
  • k up
  • l right
  • w jump to the next word
  • b jump to the previous word
  • e jump to the end of the current word
  • 0 start of line
  • $ end of line
  • gg start of file
  • G end of file
  • ) jump to the next sentence
  • ( jump to the previous sentence
  • } jump to the next paragraph
  • { jump to the previous paragraph
  • [[ jump to the previous section
  • ]] jump to the next section

Text objects and useful edits

These are some of the most important Vim commands because they let you target chunks of text precisely.

  • dw delete from the cursor to the end of the current word
  • diw delete inner word: delete the word under the cursor without the surrounding whitespace
  • daw delete a word: delete the word and the surrounding whitespace
  • cis change inner sentence
  • cas change a sentence
  • cip change inner paragraph
  • cap change a paragraph
  • ciw change inner word
  • caw change a word
  • dd delete the whole line
  • yy copy a line
  • p paste after the cursor
  • P paste before the cursor
  • x delete the character under the cursor
  • u undo
  • Ctrl+r redo

Visual mode

Visual mode is for selecting text before acting on it.

  • v start character-wise visual selection
  • V start line-wise visual selection
  • Ctrl+v start block-wise visual selection
  • d delete the selection
  • y yank the selection
  • c change the selection

You can combine visual mode with motions and text objects to make very targeted edits.

  • /term search forward
  • ?term search backward
  • n next match
  • N previous match
  • f find the next character on the current line
  • F find the previous character on the current line
  • t move to just before the next character
  • T move to just after the previous character
  • s delete the character under the cursor and enter Insert mode
  • S delete the current line and enter Insert mode
  • ; repeat the last f/t motion
  • , repeat in reverse

Example: fa jumps to the next a on the current line.

Substitution

Substitution replaces text pattern-by-pattern.

  • :s/old/new/ replace the first match on the current line
  • :s/old/new/g replace all matches on the current line
  • :%s/old/new/g replace all matches in the whole file
  • :%s/old/new/gc confirm each replacement

You can also use more advanced patterns such as :s/\s\+/ /g to collapse repeated whitespace.

Buffers, windows, and tabs

Vim works well with multiple open files and views.

  • :e file.txt open a file in a buffer
  • :bnext go to the next buffer
  • :bprev go to the previous buffer
  • :ls list buffers
  • :bd close the current buffer
  • :split split the window horizontally
  • :vsplit split the window vertically
  • Ctrl+w then h/j/k/l move between windows
  • Ctrl+w then c close the current window
  • :tabnew open a new tab
  • gt go to the next tab
  • gT go to the previous tab

Marks and registers

  • m[a-z] set a mark
  • `a jump to mark a
  • :marks list marks
  • "ay yank into register a
  • "ap paste from register a
  • :reg show registers
  • "+y copy to the system clipboard
  • "+p paste from the system clipboard

Registers are extremely useful for moving text around without losing anything.

Macros

Macros let you record a sequence of edits and replay it later.

  • q{register} start recording a macro into the chosen register
  • q stop recording
  • @{register} replay the macro once
  • @@ replay the last macro again
  • :reg inspect your registers
  • "ay$ yank the current line into register a
  • "ap paste the contents of register a

Updating a macro

A macro is just a sequence of Normal-mode commands stored in a register. You can update it by editing the register contents directly.

  1. Record a macro into register a with qa...q.
  2. Paste its contents with "ap.
  3. Edit the pasted text in Insert mode or Normal mode.
  4. Yank the updated commands back into the same register with "ay$.

This is useful when you want to append to an existing macro or refine it without re-recording everything from scratch.

Appending to a macro

To add more commands to an existing macro register, record into the same register with a capital letter:

  • qA starts recording and appends to register a
  • q stops recording

This appends to the existing macro instead of overwriting it.

.vimrc settings and useful mappings

A small .vimrc can make Vim feel much more approachable.

" Basic behaviour
set number
set relativenumber
set tabstop=4
set shiftwidth=4
set expandtab
set smartindent
set ignorecase
set smartcase
set hlsearch
set incsearch
set mouse=a

" Make escaping easier
inoremap jk <Esc>

" Quickly save and quit
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>x :wq<CR>

" Better split navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" Keep search centered
nnoremap n nzz
nnoremap N Nzz

Common options explained:

  • set number and set relativenumber show line numbers.
  • set tabstop=4 and set shiftwidth=4 make indentation more predictable.
  • set expandtab uses spaces instead of tabs.
  • set ignorecase and set smartcase make searches less annoying.
  • set hlsearch and set incsearch highlight and show matches as you type.
  • inoremap jk <Esc> lets you leave Insert mode faster.

Save, quit, and file commands

  • :w save the file
  • :q quit Vim
  • :wq save and quit
  • :q! quit without saving
  • :w newname.txt save as a new file

Quick cheat sheet

ActionShortcut
Enter Insert mode before cursori
Enter Insert mode after cursora
Open a line belowo
Return to Normal modeEsc
Move left/down/up/righth/j/k/l
Move to next wordw
Move to previous wordb
Move to next sentence)
Move to previous sentence(
Move to next paragraph}
Move to previous paragraph{
Jump to next characterf
Jump to previous characterF
Delete character and enter Insert modes
Search forward/term
Search backward?term
Next matchn
Delete worddw
Delete inner worddiw
Delete a word including surrounding whitespacedaw
Delete linedd
Copy lineyy
Pastep
Undou
RedoCtrl+r
Start visual selectionv
Start line visual selectionV
Start block visual selectionCtrl+v
Substitute on current line:s/old/new/
Substitute globally:%s/old/new/g
Start recording macroq{register}
Stop recording macroq
Replay macro@{register}
Replay last macro@@
Append to macroqA
Paste register contents"ap
Yank to register"ay
Save:w
Quit:q
Save and quit:wq

A practical workflow

  1. Open a file in Vim.
  2. Move with h/j/k/l or w/b/e.
  3. Use f/F to jump to important characters quickly.
  4. Select text with v and edit it.
  5. Use dw, diw, or daw to remove words precisely.
  6. Use ) and ( for sentence-level movement, and {/} for paragraph-level movement.
  7. Search with / and replace with :s or :%s.
  8. Split windows with :split or :vsplit when working across files.

That is enough to get started, but the real power of Vim comes from combining motions, text objects, and commands until they feel natural.