5/1/2016 - 10:45 PM

vim tricks

vim tricks

vim tricks and tips

Vim Tricks

  • ga :get ASCII code of the character under cursor
  • g<CTRL-g> :get word count
  • vimdiff :view diff between files
  • :! :run command on terminal
  • :r ! :run command and paste stdout to next line

Tags for reading source

  • :tag position the cursor on tag position
  • CTRL-] position the cursor on tag under the cursor
  • CTRL-T go back after the previous jumps
  • :ptag show tag in the preview window
  • CTRL-W } show tag under cursor in preview window
  • CTRL-W z close preview window (opened by above 2 commands)

:%TOhtml :Creates an html rendering of the current file.

Surround visual selection in quotes

Surround in single quotes:

vnoremap <leader>s' c'<C-r>"'<Esc>
vnoremap <leader>s" c"<C-r>""<Esc>

Vim Registers

vim registers tutorial

yank to and paste from given register

"<reg>y to yank into register

"<reg>p to paste from register

Erase word or line in Insert Mode

C-w: erase word

C-u: erase line

VimL Snippets

:execute "normal! gg/foo\<cr>dd"

add ; at the EOL and return to the cursor position

:execute "normal! gg" . '/\vfor .+ in .+:' . "\<cr>"

add \v to turn on 'magic regex parsing mode', similar to egrep

Helper Functions

function! Relpath(fname, basedir)
  let l:rv = substitute(a:fname, a:basedir . "/", "","")
  echom l:rv 
  return l:rv