# Introduction # * C-a == Ctrl-a * M-a == Alt-a # General # ``` :q close :w write/save :wa[!] write/save all windows [force] :wq write/save and close :x save and quit, same as wq :q! force close if file has changed and not save changes ``` ``` v Enter visual mode for selection of LINES C-v Enter visual mode for selection of BLOCKS y Yank/copy selected region yy Yank/copy entire line "y Yank/copy marked region into register (register from a-z) c Cut selection p Paste yanked content "p Paste yanked content in register (from a-z) P Paste yanked content BEFORE ``` ``` u Undo C-r Redo ``` ``` :! Execute shell command C-z send vim to background (fg brings it to front again) ``` ### Windows #### ``` C-ws Split current window horizontally (alternative :split) C-wv Split current window vertically (alternative :vsplit) C-ww Jump to the next window C-wARROW Jump to window left/right/top/bottom (arrow keys) to the current C-w#< Shrink/resize current window from the right by # (default 1) C-w#> Increase/resize current window to the right by # (default 1) ``` ### Entering insert mode ### ``` a Append text after the cursor A Append text at the end of the line i Insert text before the cursor I Insert text before the first non-blank in the line o Begin a new line BELOW the cursor and insert text O Begin a new line ABOVE the cursor and insert text, s Erase the current letter under the cursor S Erase the whole line cc Delete the current line cw Delete word. ``` ### Recording ### Vim has 26 registers (a-z), select the one you want to record in, see below. Exit Record mode with ESC ``` q[a-z] Start recording, everything will be recorded including movement actions. @[a-z] Execute the recorded actions. ``` ### vimgrep and quickfix list ### built-in grep, vimgrep uses vim's quickfix list. see vimcasts#44 for introduction: http://vimcasts.org/episodes/search-multiple-files-with-vimgrep/ ``` :vimgrep //g % Search for with multiple occasions per line (g) in current file (%) :vimgrep /// % On the command line, / (that is: CTRL-R followed by /) will insert the last search pattern. :vimgrep //g Search in the given files () :vimgrep //g *.cc Search in all *.cc files current directory :vimgrep //g **/*.cc Search in all *.cc files in every sub-directory (recursively) :vimgrep //g `find . -type f` Search in all files that are returns by the backtick command. :vim short for :vimgrep :cnext Jump to next record/match in quickfix list :cprev Jump to previous record/match in quickfix list ``` Unimpaired plugin (https://github.com/tpope/vim-unimpaired) provides the following mappings: ``` [q see :cprev ]q see :cnext [Q see :cfirst ]Q see :clast ``` see also: http://usevim.com/2012/08/24/vim101-quickfix/ and http://vimdoc.sourceforge.net/htmldoc/quickfix.html ### Spell checking ### See vimcast #19 as an introduction: http://vimcasts.org/episodes/spell-checking/ Assuming that you have the following in .vimrc: ``` nnoremap s :set spell! ``` ``` s Toggle Spelling ]s Next spelling mistake [s Previous spelling mistake z= Give Suggestions (prepent 1, use first suggestions automatically) zg Add misspelled to spellfile zug Remove word from spellfile ``` see http://vimdoc.sourceforge.net/htmldoc/spell.html # Navigation # _essential_ ``` h cursor left j cursor down l cursor right k cursor up ``` ``` H Jump to TOP of screen M Jump to MIDDLE of screen L Jump to BOTTOM of screen C-b Move back one full screen (page up) C-f Move forward one full screen (page down) C-d Move forward 1/2 screen C-u Move back (up) 1/2 screen ``` ``` w jump by start of words (punctuation considered words) e jump to end of words (punctuation considered words) b jump backward by words (punctuation considered words) 0 (zero) start of line ^ first non-blank character of line $ end of line G bottom of file gg top of file ``` _good to know_ ``` E jump to end of words (no punctuation) W jump by words (spaces separate words) B jump backward by words (no punctuation) #G goto line # #gg goto line # ``` # Search, jump # ``` * search for word under cursor (forward) and highlight occurrence (see incsearch, hlsearch below) % jump from open/close ( / #if / ( / { to corresponding ) / #endif / } [{ jump to start of current code block ]} jump to end of current code block gd jump to var declaration (see incsearch, hlsearch below) f Find char from current cursor position ``` # Editing # ``` x Delete char UNDER cursor X Delete char BEFORE cursor #x Delete the next # chars. starting from char under cursor dw Delete next word dW Delete UP TO the next word d^ Delete up unto the beginning of the line d$ Delete until end of the line D See d$, delete until end of the line dd Delete whole line ``` ``` C-n Keyword completion Tab Keyword completion (SuperTab plugin) r Replace char #r Replace follow # chars with , : csock, cursor on s, 3re ceeek ``` ``` :s/xxx/yyy/ Replace xxx with yyy at the first occurrence :s/xxx/yyy/g Replace xxx with yyy first occurrence, global (whole sentence) :s/xxx/yyy/gc Replace xxx with yyy global with confirm :%s/xxx/yyy/g Replace xxx with yyy global in the whole file ``` ``` :g/^#/d Delete all lines that begins with # :g/^$/d Delete all lines that are empty ``` # Key sequences # #### Replace a word in a number of occurrences with 'bar'; use word under cursor (`*` or `/foo`) #### `* cw bar ESC n .` ``` * word under cursor 'foo' cw change word (enter insert mode) bar typed new word 'bar' ESC exit insert mode n next occurrence . repeat previous command ``` #### Insert 3 times "Help!": `Help! Help! Help! ` #### `3i Help!_ ESC` #### Insert previously yanked text in line after current #### `oESCp` #### Search for selected text #### ` Select text in VISUAL mode (v) y Yank selection / Search for C-r0 Press Ctrl-R and 0 to paste in ``` #### Comment out selection #### `C-v