vimscript - The love/hate relationship

by Lowe Thiderman

vimscript.ninjaloot.se

So, wtf is vimscript?

Everyone in here has used it!

  • Everyones .vimrc file
  • Syntax highlighting
  • Automatic indentation
  • Folding
  • ...most vim features, actually

vimscript is the programmable interface to vim

(in two parts)

  • Basic commands
    • :set, :e, :w
  • Programming language
    • ...with all that that entails

So what's wrong with it?

  • It's a bastard language
  • A lot of common things are really irritating
    function! Shout(msg)
      echo msg . '!'
    endfunction
  • Errors can be seriously hard to trail

But what's awesome with it then? :3

It can do everything that vim can do!

...and it can do it a million times faster than you can!

Function example!

Last changed: 2013-01-09 19:31:31 GMT +0100

function! Timestamp()
  let match = '\v\C%(<Last changed\s*:\s+) <TIMESTAMP>'
  let repl  = '%Y-%m-%d %H:%M:%S %z %Z'

  for linenr in range(1, 20)
    let line = getline(linenr)
    if line =~ match
      let newline = substitute(line, match, strftime(repl), '')
      return setline(linenr, newline)
    endif
  endfor
endfunction
:call Timestamp()

Best feature evar:
Autocommands!

  • Make vim automatically do things for you
  • Over a hundred events defined
    • Before and after loading and saving files
    • Entering and exiting insert mode, not moving the cursor for a while
    • Getting or losing focus to the vim window

Autocommand examples!!

au BufWritePost *.json echo "Wrote a JSON file, lol!"
au BufWritePre * call Timestamp()
au BufRead,BufNewFile *nginx/*.conf set ft=nginx

au FileType nginx set foldmethod=marker foldmarker={,}
au FileType nginx set shiftwidth=4 softtabstop=4 noexpandtab
au FileType nginx set commentstring=#%s

Learn moar!

Get help!

#vim @ irc.freenode.net

@thiderman on twitter!

thx <3