public
Created

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

  • Download Gist
pres.mkd
Markdown

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 :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


" 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


:Nih!

" vim: ft=vim

pres.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.