Create a gist now

Instantly share code, notes, and snippets.

Scripting Vim - From Moves to Tools [A Vim Presentation]

Scripting Vim - From Moves to Tools

  • Customizing Vim
  • Mappings
  • Commands
  • Batching with Vim
  • Case: plugin

" Common Behaviour

Any sufficiently advanced use of Vim is indistinguishable from scripting.

  • Map your common moves
  • Create high-level actions as commands
  • Compose sequences and flow as functions

" Moves: less writing, more editing

" Pour some Coffee in your JS

console.log("hello", "world")

console.log "hello", "world"


" Macros: just for now



" Customize Vim

  • $HOME/.vim

A plethora of settings

  • plugin: options, mappings, commands...
  • ftplugin: -||- by filetype (e.g. completion)
  • indent, syntax, compiler, spell, colors...

" Help

:help :help :help 'encoding' :help f

" A Note on Plugin Management

Pathogen - Extending the runtime path

" Your mappings and commands

  • <~/.vimrc>
  • <~/.gvimrc>
  • <~/.vim/>
  • <~/.vim/plugin/>
  • <~/.vim/bundle/vimheap/plugin/abbrs_maps.vim>
  • <~/.vim/bundle/vimheap/plugin/cleanup.vim>

" Mappings

" delete parens of call
nnoremap dC f(%x``r<Space>b

" "Delete Lisp": delete list-style function calls
nnoremap dl F(%x``xdw=``

noremap <Leader>tw :%s/\s\+$//g<CR>``:noh<CR>

inoremap ^/ <ESC>T<yt>o</<ESC>pF<EylD"0pa>

inoremap ^> <Esc>:s/.*<\([^ ]\+\)[^>]*>.*/&<\/\1>/ \| noh \| <CR>A

nnoremap <F3> :if &buftype == "quickfix"<Bar>cclose<Bar>else<Bar>botright copen<Bar>endif<CR>

nnoremap <S-F3> :silent make <Bar> botright cwindow<CR>

" Commands: high-level actions

command! -nargs=0 ImportCleanup :g/^import \|^from /exec "norm $b"|if(!search('\<'.expand("<cword>").'\>', 'nWp'))|d|en|noh

" Scripts: just source them


" Scripts: This Presentation

" Batching: run scripts with vim

:cd your-project/
:arg **/*.py
:arg `ack -l simplejson`
:argdo %s/^import\s+simplejson\>/import json/ge | up

" opt. :set hidden -> argdo -> preview -> :wa

" cmdline mode

$ vim -esc '%s/Item/ITEM/ | upd' FILE
$ vim -es < modify.vim FILE

" Mail your edits

$ vim -es FILE <<-EOF
    normal ~

$ curl -O

$ vim -E -s examples.txt <<-EOF
    %s/itemscope itemtype="[^"]*\/\([^"]\+\)"/typeof="\1"/g
    %s#WITH\s\+MARKUP:\_s\+<\w\+#& vocab=""#
    %s/property="itemListElement"/& inlist/g
    g/<meta.\+property="\(url\|contentURL\|thumbnail\)"/s/<meta \(.*\)content\s*\(="[^"]\+"\)/<link \1href\2/
    :saveas examples-rdfa.txt

" Outside the box

Filter through shell cmd

:%!sort | uniq -d



" Case: An example plugin

  • Documentation
  • Configurability
  • VimScript OO using dicts


" vim: ft=vim

set ls=1
set cmdheight=2
set nonumber
set guioptions=gmteA
set guifont=Menlo\ Regular:h32
"set fullscreen
"set showtabline=0
set columns=72
set transparency=64
colorscheme tinfoil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment