Vim

Vim

vim essentials !

General

  • ESC / CTRL+c : Command Mode
  • :q : Quit
  • :q! : Quit WITHOUT Saving
  • :wq / :x : Save and Quit
  • :w new_file_name : Save content to a new_file_name
  • :#,#w new_file_name : Save content starting from # to # to new_file_name

Navigation

Character

  • h : Left
  • l : Right

Line

  • j : Down
  • k : Up
  • :# / #G : Jump to line Number #
  • 0 / ^ : Jump to Beginning of the line
  • $ : Jump to end of the line

Word

  • w : Next Word(Beginning)
  • e : Next Word(End)
  • b : Previous Word(Beginning)

Sentence

  • ) :Jump to Beginning of next sentence
  • ( :Jump to Beginning of previous sentence

Paragraph

  • } : Jump to Beginning of next paragraph
  • { : Jump to Beginning of previous paragraph

Page

  • G : Jump to end of the document
  • gg : Jump to start of the document
  • CTRL+f : Scroll Next Page
  • CTRL+b : Scroll Previous Page

Inserting

  • i : Insert before cursor
  • I : Insert at the beginning of the line
  • a : Insert after the cursor
  • A : Insert at the end of the line
  • o : Insert a new line below the cursor
  • O : Insert a new line above the cursor
  • :r file_name : Insert the external file_name content at the cursor location
  • :#r file_name : Insert the external file_name content at the line no #

Editing

Cut, Copy & Paste

  • d : delete the Character

  • dw : delete start of next word

  • de : delete till end of current word

  • dd : delete whole line

  • d) : delete rest of the sentence

  • dt? : delete until ?

  • y : copy the selection characters

  • y) : copy rest of the sentence

  • ye : copy still end of current word

  • yy : copy the while line

  • x : cut Character

  • p : paste the selection/copied after the cursor

Selection

  • v : enter selection mode
  • V : select line
  • CTRL+V : block selection

Replace

  • r : replace characters
  • c : replace entire line
  • cw : change word and delete to insert
  • ce : replace rest of the word
  • c) : replace rest of the sentence
  • cc : replace whole line
  • ct? : delete until ? and replace with
  • c$ : delete until the rest of the line

verb-modifier-object

VerbModifierObject
v (visual)i (insert)w (word)
c (change)a (around)s (sentence)
d (delete)t (till)p (paragraph)
y (yank)f (find)b (block)
t (tag)
  • ciw : change current word
  • ct<CHAR> : delete until
  • diw : delete current word
  • c/foo : change until next occurrence of foo

Undo, Redo & Repeat

  • u : Undo
  • CTRL+R : Redo
  • . : repeat the last command

Searching

  • /pattern : search pattern in forward direction
  • ?pattern : search pattern in the backward direction
  • n : jump to next occurrence
  • N : jump to previous occurrence

Search and Replace

  • :s/old/new/g : replace old with new
  • :#,#s/old/new/g : replace old with new from line number # to line number #
  • :%s/old/new/gc : replace old with new with prompt in whole file

Miscellaneous

Jumps

  • Ctrl+i : move to next instance in jump list
  • Ctrl+o : move to previous instance in jump list
  • % : jump to pair of the bracket
  • jumps : list all the jumps
  • Shift+G : show current file locations
  • zz : move the cursor to middle of window

Register

  • "ay : copy to register a
  • "ap : paste from register a
  • :reg : see list of registers

Macros

  • qa : record macro a
  • q : stop recording macro
  • @a : run macro a
  • @@ : run last macro again

Marks

  • m[a-z] : mark text using character mode (from a to z)
  • M[a-z] : mark lines using line mode (from a to z)
  • 'a : jump to position marked a
  • '. : jump to the last edit
  • :marks : list all the marks

Split view

  • :sp / Ctrl+ws : split view (open new window/ same buffer) Horizontal
  • :vsp : split vertically
  • Ctrl + ww : switch between windows
  • Ctrl + wc : close window
  • Ctrl + h/j/k/l : navigating between the splits

Buffers

  • :e file : edit file in new buffer
  • :e! : reload file from disc
  • Ctrl+g : show file name and cursor location
  • :ls : see list of all buffers
  • :bn : move to next buffer
  • :bp : move to previous buffer
  • :b file : move to a specific buffer “file”
  • :b # : move to buffer number #

Tabs

  • :tabnew : open new tab
  • :tabnew file : open file in new tab
  • :tabn : next tab
  • :tabp : previous tab

Set Options

  • :set nu : enable line numbers

  • :set rnu : enable relative line numbers

  • :set rnu! : disable relative line numbers

  • :set nonu : disable line numbers

  • :set ruler : enable the file ruler

  • :set autoindent : autoindent

  • :set expandtab : expand tab to spaces

  • :set shiftwidth=4 : when shifting indent with 4 spaces

  • :set tabstop=4 : when tabbing indent with 4 spaces

  • :set hlsearch : enable search highlights

  • :set ignorecase : ignore search case

  • :set incsearch : Incremental search with partial matches