Vim / Vi Essentials - CodeSwordsman

A quick reference for modes, motions, editing, search and replace, splits and common settings, with notes on vi versus vim compatibility.

Vim 8 / 9 (vi compatible)Updated 2026-10-3052 commands

Startup & Modes

Open a file in vim; a missing file is created, with the cursor on line 1

vim /etc/nginx/nginx.conf

Open a file at line 42, handy when jumping to an error line

vim +42 app.log

Enter insert mode before the cursor; a inserts after, o opens a new line below

i

Return to normal mode, where all motion and editing commands work

Esc

Enter character-wise, line-wise or block-wise visual mode before acting on a selection

v / V / Ctrl+v
Risky

Save and quit; :x and ZZ behave the same and only write when the buffer changed

:wq
Risky

Quit and discard every change; accidental use loses unsaved work

:q!

Cursor Motions

Move left, down, up, right by one character; prefix a count such as 5j to move five lines

h  j  k  l

Jump to the next word, the previous word, or the end of the current word

w  b  e

Jump to the start of the line, its first non-blank character, or the end

0  ^  $

Jump to the first line, the last line, or a specific line number

gg  G  :42

Scroll half a page down or up; Ctrl+f and Ctrl+b scroll a full page

Ctrl+d  Ctrl+u

Jump by paragraph, delimited by blank lines, which is fast in config files

{  }

f jumps to the next x in the line, t to the character before it, ; repeats the jump

f x  t x  ;

Jump between matching brackets, useful when reading scripts

%

Editing & Deleting

Delete the character under the cursor or the one before it

x  X

Delete a line, yank a line, paste below or paste above

dd  yy  p  P

Delete to the next word, to the end of the line, or to the start; prefix a count to repeat

dw  d$  d0

Change the current word and enter insert mode, the everyday way to edit a setting

cw

Undo the last change, or redo an undone change

u  Ctrl+r

Indent or outdent the current line; works on a visual selection as well

>>  <<

Join the next line into the current one, useful for wrapped settings

J

r replaces one character and returns to normal mode; R keeps replacing until Esc

r  R

Search & Replace

Search downward; use ? to search upward, then n and N to jump between matches

/error

Search for the word under the cursor downward with * or upward with #

*  #

Replace every match in the file; check the scope with :set number first

:%s/old/new/g

Confirm each replacement: y replaces, n skips and a replaces the rest

:%s/old/new/gc
Risky

Delete every line matching the pattern; preview with :g/pattern/p first

:g/pattern/d

Clear the highlight left by the last search

:noh

Visual Mode & Macros

Select whole lines and delete them; a selection can also be yanked or indented

V  j  j  d

Block-select several lines, insert a comment marker, then Esc to apply it everywhere

Ctrl+v  j  j  I  #  Esc

Replace only inside the previous visual selection

:'<,'>s/old/new/g

Start recording a macro into register a; press q again to stop

qa

Run the macro in register a; @@ repeats the last executed macro

@a  @@

Splits & Tabs

Split horizontally to open another file; use :vsp to split vertically

:sp /etc/hosts

Move between split windows by direction

Ctrl+w  h / j / k / l

Equalize split sizes

Ctrl+w  =

Open a new tab; gt and gT switch to the next and previous tab

:tabnew  gt  gT

Keep only the current window, close the current one, or :qa to quit all

:only  :q

Saving & Files

Write the current file without quitting

:w

Write a copy to another path, a cheap backup before editing configs

:w /tmp/nginx.conf.bak

Write back with elevated rights when the file was opened without sudo

:w !sudo tee %

Reload the file and discard local changes, useful after external edits

:e!

Insert the contents of another file below the cursor

:r /etc/hosts

Convert CRLF to LF, fixing bad interpreter errors for scripts copied from Windows

:set ff=unix

Settings & vi Differences

Edit the personal config file; set commands there apply on every start

vim ~/.vimrc

Show line numbers; :set relativenumber shows relative numbers for fast jumps

:set number

Set tab width 4, shift width 4 and expand tabs to spaces

:set ts=4 sw=4 et

Enable before pasting so auto-indent does not cascade; turn it off with :set nopaste

:set paste

Turn on syntax highlighting

:syntax on

Leave vi-compatible mode to enable undo trees, completion and plugins

:set nocompatible

On most distributions vi is a symlink to vim, so behavior depends on compatible mode; classic vi lacks extended :set options and multi-level undo

Start the built-in interactive tutorial; use :help for the full manual

vimtutor

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.