Contact Info

(for those who care)

Instant Gratification   

Wed, 12 Mar 2014

Simple vim Templates

I was messing with my .vimrc and ran across this kindof cool technique which is worth sharing. Templates!

I’m usually not a fan of automatic code generation or templates / snippets but I’ve found a pretty good way to integrate them into my workflow.

" snippet: read in various templates
nmap <leader>t :read ~/bin/vim-template-more-test<cr>
nmap <leader>x :read ~/bin/vim-template-fixme<cr>
nmap <leader>m :read ~/bin/vim-template-perl-module<cr>
nmap <leader>f :read ~/bin/vim-template-perl-function<cr>

…and you’re done! This is about the simplest use of templates and I’m OK with it. Your “templates” are just text files (I store them in my ~/bin directory for convenience), and you wire them up to certain keystrokes.

Since I’ve been writing a lot of perl lately, and perl is notoriously baroque, and DOUBLY so for object-oriented perl, I needed a template to create a “fully proper” OO perl module. PackageName, return 1, bless ( $self, $class ), the full monte that I can’t be bothered to remember every single time I need to make a new module. ,m Bam! Module! ,f Bam! Function!

It has dual benefit of saving keystrokes and making sure I’m getting the basics right.

…but that wouldn’t be the only thing you’d want to do. Another baroque thing about perl is POD documentation (think pre-pre-pre-javadocs). I can never remember the syntax, so I put together a crazy macro which snags the function name under the cursor and starts off a perldoc comment above it. ,d Bam! Documentation!

" snippet: <leader>d == document perl function with crazy perldoc
nmap <leader>d 0/sub /e+1<cr>yiwki=item B<<c-r>"><cr><cr>=cut<esc>ki

And on a final note, here’s another useful macro:

" select previously inserted text
nmap <leader>v `[V`]

…if you’ve been paying attention you can see where this is going.

,f      - insert function template
,v      - select previously inserted text
:s/XXX/functionName/g   - replace placeholder text

But even better:

nmap <leader>F viwx:read ~/bin/vim-template-perl-function<cr>`[V`]:s/XXX/<c-r>"/g<cr>

…and the explanation for those playing along at home:

nmap       - normal mode map
<leader>F  - to capital "F"
viw        - visual inner word
x          - delete it(!)
:read ~/bin/vim-template-perl-function<cr>
`[V`]      - line-select the previously inserted text (begin/end markers)
:s/XXX     - substitute XXX
  /<c-r>"       - recall the paste buffer (what we deleted with 'x')
  /g<cr>        - do it globally

I can’t think of a good reason to not use the initial technique of :nmap <leder>a :read ~/bin/template-a.txt<cr>. It is so clear and generically useful it’s almost a necessity.

The reason to avoid the other two techniques is that you end up with complicated vim macros which can be a pain to maintain. However, they do build on relatively simple predecessors, generally all follow the same pattern, and have huge potential keystroke savings, so I would recommend trying it out.

15:08 CST | category / entries
permanent link | comments?

Like what you just read? Subscribe to a syndicated feed of my weblog, brought to you by the wonders of RSS.

Thanks for Visiting!