Skip to content

Instantly share code, notes, and snippets.

@niklasl
Created January 10, 2013 21:41
Show Gist options
  • Save niklasl/4506042 to your computer and use it in GitHub Desktop.
Save niklasl/4506042 to your computer and use it in GitHub Desktop.
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"
"f(%x``r<Space>b
------------------------------------------
" Macros: just for now
qa...q
@a
"ap
"ay$
------------------------------------------
" Customize Vim
- $VIMRUNTIME
- $HOME/.vim
A plethora of settings
* plugin: options, mappings, commands...
* ftplugin: -||- by filetype (e.g. completion)
* indent, syntax, compiler, spell, colors...
------------------------------------------
" Help
:help
:help <topic, keyword, option, command, function, ...>
: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
:so
:source
------------------------------------------
" Scripts: This Presentation
- <pres.vim>
- <bundle/vimheap/plugin/presfold.vim>
------------------------------------------
" 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 ~
EOF
------------------------------------------
$ curl -O https://dvcs.w3.org/hg/webschema/raw-file/0bb6f6823c45/schema.org/drafts/examples/site/examples.txt
$ vim -E -s examples.txt <<-EOF
%s/itemscope itemtype="[^"]*schema.org\/\([^"]\+\)"/typeof="\1"/g
%s#WITH\s\+MARKUP:\_s\+<\w\+#& vocab="http://schema.org/"#
%s/\<itemprop\>=/property=/g
%s/property="itemListElement"/& inlist/g
g/<meta.\+property="\(url\|contentURL\|thumbnail\)"/s/<meta \(.*\)content\s*\(="[^"]\+"\)/<link \1href\2/
:saveas examples-rdfa.txt
:q
EOF
------------------------------------------
" Outside the box
Filter through shell cmd
:%!sort | uniq -d
:%!md5
:%!bash
------------------------------------------
" Case: An example plugin
* Documentation
* Configurability
* VimScript OO using dicts
- <bundle/vim-toner/>
------------------------------------------
:Nih!
------------------------------------------
" vim: ft=vim
set ls=1
Nocursorline
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
PresFold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment