## 1 Some useful Vim tricks

• Remove all empty lines in a file.

:%s/^\s*$//gc  • Redirect command line output to buffer. :put =expand('%:p') :.,+2put =expand('%:p')  • Use <C-r>= to remove new line, the output will be added in the cursor position. # in insert mode <C-r>= RET expand('%:p') RET  ## 2 non-greedy • non-greedy matches, it matches as less as possible • * => match as many as possible • \{-} => match as less as possible • e.g. matches all html from open-angle-bracket to close-angle-bracket • :%s/<.\{-}> • delete line contain pattern from pat1 to pat2 :g/pat1/,/pat2/d  • Range with global :global /pat1/,/pat2/ cmd :global /pat1/,/pat2/ s/\d\+//gc  • Delete line contains pattern :g/pat/d  • Delete line Does not conain pattern :g!/pat/d  • Select current word in Vim script :exec "normal vaw"  • Run man page inside Vim :Main grep  • Append content to register :"Ayy  • Redierct Ex command output to register [h redir] :redir @a | ls | redir END :redir @a | echo expand('%:p') | redir END  • Redirect Ex command outuput to a file :redir > /tmp/f.x | ls | redir END  " how to append content to register :"Ayy " split String to list split(str, '\zs') " redirect Ex command output to variable var :redir =>var |g/pattern/p | redir END " replace word with current buffer file name [h expand()], [h \=] :s/word/\=expand('%')/gc " increase number by one [h \=, :h submatch()] :s/\d\+/\=submatch(0) + 1/gc " select word under cursor &ltC-R&gt&ltC-W&gt " select the current character /\%# " select the current word under cursor /\k*\%#\k* " select the iskeyword character from 0 to 10 column /\k*\%10c " redirect shell output to current buffer [h :r] r => read :r! ls " move cursor to top/bottom of page &ltS-h&gt &ltS-l&gt " add file path to register :let @a=expand("%:p") " open file inside the register in Ex mode :exec 'edit ' @a " move lines to different location, move line 2 to line 4 to top :2, 4 m 0 " filter g [match] and v or g! [not match] move lines contains pat to line 9 :g/pat/ m 9 " move lines doesn't contains pat to line 9 :v/pat/ m 9 " range 2 to 10 contains pat are moved to 9 :2, 10 g/pat/ m 9 " A sequence of optionally matched atoms, " it matches as much of list of atoms it contains as possible. \%[] /r\%[ead] match r, re rea read [] /r[ead] match re ra re " commands that save Vim folding states in vimrc file autocmd BufWinLeave . mkview autocmd BufWinEnter . silent loadview " delete n lines from the pattern matched in all buffers :bufdo! exec "/pattern" | ".,+n .d" | update  is chain two commands " stdin to vim ls | vim - • vim accepts stdin " Vimrc file multiline String let @*='first line \n \ second line' " Open URL from Vim gx " Open file from Vim gf " Start of line [^] :./^\s*\w.*\w$//gc

" Start of line [\_^], can be used anywhere in the pattern :./^\s*\w.*\w$//gc " Vim match negative matching, delete all lines don't contains cat :g!/cat/d " Substitute Visual mode, [something] 's/\%V.*\%V/[\0]/gc " Select from current line to next pattern "foo" v/foo " Select current paragraph or all consecutive lines vap " Show lines match word under cursor [I " Command Line autocomplete :h subs&lt;C-D&gt; and &lt;Tab&gt; " Copy Column and Paste &lt;C-V&gt; select column and paste it Add undo persistence in Vim set undofile " Save undo's after file closes set undodir=$HOME/.vim/undo " where to save undo histories set undolevels=1000 " How many undos set undoreload=10000 " number of lines to save for undo [:h undo-persistene]

Operators in Vim

= indentation e.g gg=G [h =] c change d delete y yank into register (does not change the text) gu make lowercase gU make uppercase ! filter through an external program gq text formatting g? ROT13 encoding > shift right < shift left zf define a fold g@ call function set with the 'operatorfunc' option

Left-right motion h character to the Left l character to the Right 0 to the first character of the line ^ to the first non-bank character of the line \$ to the end non-bank character of the line g_ to the last non-bank character of the line