Table of Contents

1 Some useful Vim tricks

  • Remove all empty lines in a file.

  • 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
  • Range with global

    :global /pat1/,/pat2/ cmd
    :global /pat1/,/pat2/ s/\d\+//gc
  • Delete line contains pattern

  • Delete line Does not conain pattern

  • Select current word in Vim script

    :exec "normal vaw" 
  • Run man page inside Vim

    :Main grep
  • Append content to register

  • 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

You need to be a leader, mapleader, localLeader maplocalleader

:nnoremap &#60;Buffer&#62;&#60;Leader&#62;s <span class="cmd">:%s/dog/cat/g</span> &#60;Esc&#62; Leader is special string that you can use to map character to shortcut key<br> The default mapping is '\' character When you type \s the shortcut will trigger <span class="cmd">:%s/dog/cat/g</span> If you want to change the default mapping to different character, then you can type<br> :let mapleader="," which will map ,s instead of \s and it can trigger <span class="cmd">:%s/dog/cat/g</span><br> LocalLeader is similar to Leader but it is to be used for mapping which are local to a buffer :map &#60;buffer&#62;&#60;LocalLeader&#62;A <span class="cmd">:%s/dog/cat/g</span> &#60;Esc&#62;

How to use command History in Vim Today, I just learn something new on how to use <span class="wbold">Command History</span> in Vim. If you are typing a lots in Ex mode, then there are lots repeating commands that you want to call it again and again. There are lots of way to do in Vim such as Abbreviation or Map. But the quick way to repeating the command is to open the history and search the command, then press Eneter

  1. Open command history q:
  2. Search the command like Normal Mode
  3. Press <span class="wbold">Enter to execute the command</span> under your cursor. Done
  4. [:h q:] [:h q/] [:h q?]

Author: cat

Created: 2019-12-01 Sun 14:53