Nithin Bekal About

Notes / Vim

Cheatsheet

Vim filters tips; in command mode:

" Read results of a command to cursor location:
r !pwd

" Read results of a command to line 42:
42r !pwd

" Use ruby to upcase entire file
%!ruby -ne 'puts $_.upcase'

" Use ruby to upcase current line (also works for visually selected line)
!!ruby -ne 'puts $_.upcase'

Diff

Compare and diff a file on current branch with master branch version:

  • Open foo.rb right tab
  • Open foo.rb from master in the left tab
    • :Gedit master:foo.rb
  • Diff the files using :windo diffthis
  • Close diff view with :diffoff

Vimscript

function HelloWorld()
  echo "Hello, world!"
endfunction

call HelloWorld()


function ReturnSomethign()
  return "Something"
endfunction

echo ReturnSomethign()

function Foo()
endfunction
" Returns 0 by default

function TooBigEh(foo)
  if a:foo > 10
    return 1
  end
endfunction
" Returns 1 if cond matches, falls back to 0 otherwise
" a:foo is for variable scoping - tells vimscript that it's the argument foo


" Truthiness
"  'hey' is coerced in to 0, so falsey
"  '8 hello' is coerced to 8 because it starts with a number, so truthy


" Local vars
function Foo()
  let bar = 'baz'
  return bar
endfunction


" Match url in the following line:
" foo bar http://hello.baz hai
" Matches string starting with http, until it runs into spaces
matchstr(getline('.', "http[^ ]*"))

Screencasts

Other vim implementations

Books

Videos: