Vim cheat sheet

Famous command line editor

General

  • :help – Enter help
  • <C-]> – Help enter a menu
  • <C-[> – Help back / escape
  • Ctrl + [ / Ctrl + c – Escape alternative
  • :%s/find_what/replace_to/g – find and replace all
  • :set paste – paste without autoindent, :set nopaste turn on autoindent
  • u – undo, Ctrl+r – redo
  • . – Repeat previous action
  • :set number / :set nonumber – add / remove lines numbers
  • ~/.vimrc – vim defaults. e.g. add line ‘set number’ to have line numbers by default
  • Esc -> Ctrl + z – Run vim in background and back to terminal.
    $ fg – to bring vim back to foreground
  • >> / << – indent right / left (in normal mode)
  • Ctrl + T / Ctrl + D – indent right / left (in insert mode)
  • 3>> / 3<< or >2j / <2j indent 3 lines right / left
  • >} – Indent from cursor to next blank line
  • >aB – Indent current block structure
  • == – Automatically indent current line
  • gg=G – Reindent entire buffer
  • Ctrl + V -> select before lines -> I -> # -> Esc – Comment block of code
  • :edit file-name.txt – edit new file in the same window

.vimrc

inoremap jk <esc>

Navigation

  • h,j,k,l – Move Left, Down, Up, Right
  • Ctrl + y – Scroll up
  • Ctrl + e – Scroll down
  • Ctrl + u – Move half screen up
  • Ctrl + d – Move half screen down
  • H – Move to top of screen
  • M – Move to middle of screen
  • L – Move to bottom of line
  • 0 – Move to start of line
  • 123G – Go to line 123
  • G – Move to last line
  • ^ – Move to the start of text in line
  • $ – Move to the end of line
  • gg – Go to file start (ctrl + home)
  • # go to other occurrence of current word

Insert / Append

  • i – start insert mode at cursor
  • I – insert at the beginning of the line
  • a – append after the cursor
  • A – append at the end of the line
  • o – open (append) blank line below current line (no need to press return)
  • O – open blank line above current line
  • ea – append at end of word
  • r – replace one char
  • R – replace text

Edit text

  • x – delete current char
  • X – delete char before, like backspace
  • dw – Delete word
  • dG – Delete all to the end of file
  • d$, D – delete to the end of line
  • v – Select text. While selecting text, you can perform searches and other advanced movement.
  • V – Select text by lines
  • Ctrl + v – Select block
  • d – Cut / Delete
  • y – Copy (Yank)
  • p – Paste after current line
  • P – Paste before the cursor
  • dG – Delete all to the end of file
  • yy – Copy line
  • yy, p – Duplicate line
  • di” – Delete in quotes
  • da” – Delete in quotes including quotes
  • yiw->viwp – Copy one word and paste it to another

Tabs

  • :tabedit / tabe somefile.txt – open new tab
  • gt / gT – move next / previous tab
  • 1gt, 2gt etc – move to tab 1, 2 etc
  • :tabfirst, :tablast, :tabn, :tabp – Move to tab first, last, next, previous
  • :tabs – list of tabs
  • $ vim -p file1 file2 – open multiple files in tabs
  • :tabclose – close current tab
  • :tabonly – close all tabs except of current
  • :mksession myproject.vim- save open tabs workspace in a session
  • :mks! – save workspace to file Session.vim
  • $ vim -S Session.vim – open workspace from session
  • :qa – quit all tabs
  • :wqa – save and quit all tabs

Search / Replace

  • /pattern – search for pattern
  • ?pattern – search backward for pattern
  • n – repeat search in same direction
  • N – repeat search in opposite direction
  • ggn – go to first match
  • GN – go to last match
  • :%s/old/new/g – replace all old with new throughout file
  • :%s/old/new/gc – replace all old with new throughout file with confirmations
  • * / # – Search exact word next / previous
  • g* / g# – Search word containing this word next / previous

Windows & Buffers

  • :edit – Open file in new buffer
  • :badd filename – add file to new buffer
  • :bn / :bp – Next / previous buffer
  • :term – Open terminal in new buffer
  • <C-w>v – New split window vertically
  • <C-w>s – New split window horizontally
  • <C-w>h/j/k/l – Move between windows
  • Ctrl + WW – Move next buffer
  • <C-\> <Ctrl-n> – exit terminal mode
  • <C-w> <C-r> – Swap parts of split window
  • <C-w>K – Restore split windows positions
  • :res 10 – resize horizontal height 10 (or :resize)
  • :res +5 / -5 – resize horizontal height +5 / -5
  • :vertical resize 80 / +5 / -5 – vertical resize to 80 / +5 / -5
  • <C-w>- / <C-w>+ resize horizontal split
  • <C-w>> / <C-w>< resize virtical split
  • <C-w> 10 +/-/</> resize 10 steps
  • <C-w> = – Resize all windows to equal
  • <C-w> _ / | – Maximize horizontal / vertical split

Edit multiple files

  • :args – List of open files
  • :argadd filename – Add file to open files list
  • :argedit filename – Add file to args and start edit it
  • :n / :N – Next / previous file
  • :wn / :wN – Save and move next / previous file
  • :w! / :N! – Move to next / previous file without change
  • :e# – Swap between files
  • :set autowrite – Saves files automatically when moving to another file

File explorer

https://shapeshed.com/vim-netrw/

  • :Vex – open netrw in new vertical split

Links:
http://www.worldtimzone.com/res/vi.html
https://www.youtube.com/watch?v=5r6yzFEXajQ

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s