note
Smylers
<p>I gave a talk on <em>Vim</em> tips and tricks for Perl programmers at YAPC Europe in Belfast last year. Here's some of the useful config I mentioned.</p>
<ul>
<li><p>Activate the Perl-filetype settings that come with <em>Vim</em> for many types of files, including Perl code:</p>
<code>
filetype plugin on
</code>
<p>You can put your own config that you want applying only to Perl files in <em>~/.vim/after/ftplugin/perl.vim</em>; this will be applied after the system-wide settings (so can override them). You should use <code>setlocal</code> rather than <code>set</code> in such files, so that any settings don't 'leak out' to windows that are opened later for other types of files.</p>
</li>
<li><p>Indenting — I have these directly in my <em>~/.vimrc</em>, applying to all filetypes:</p>
<code>
filetype indent on " per-filetype config
set tabstop=8
set expandtab
set smarttab
set shiftwidth=4 " or 2 or whatever
set shiftround
set autoindent
</code>
<p>Note that <code>smarttab</code> means that <code>BkSpc</code> at the beginning of the line will outdent by one level (not just delete a single space character), so that <code>Tab</code> and <code>BkSpc</code> together 'feel' like they're operating on tab characters, even though only spaces appear in your file. I leave <code>tabstop</code> set at 8, since that makes it more obvious when other people give you files with nasty tab characters in them!.</p>
</li>
<li><p>Navigating between files — put this in your <em>~/.vim/after/ftplugin/perl.vim</em> and then when the cursor is on a module name (such as <code>File::Temp</code>) you can press <code>Ctrl+W f</code> to open the source (<em>...wherever/lib/File/Temp.pm</em>) of that module in a split window, or <code>gf</code> to load it in the current window:</p>
<code>
setlocal isfname+=:
</code>
</li>
<li><p>Mappings — keystrokes to execute common commands. Save and check syntax with <code>_c</code>:</p>
<code>
nnoremap <buffer> <silent> _c :w<Enter>:!perl -wc %<Enter>
</code>
<p>Look up docs function under cusor with <code>_f</code>:</p>
<code>
nnoremap <buffer> <silent> _f :perldoc -f <cword><Enter>
</code>
<p>Look up docs for module under cusor with <code>_m</code>:</p>
<code>
nnoremap <buffer> <silent> _m :perldoc <cword><Enter>
</code>
<p>Tidy selected lines (or entire file) with <code>_t</code>:</p>
<code>
nnoremap <buffer> <silent> _t :%!perltidy -q<Enter>
vnoremap <buffer> <silent> _t :!perltidy -q<Enter>
</code>
<p>If you're using <em>Vim</em>'s own gui (not running it in a terminal window) then running <code>perldoc</code> may try to invoke a pager that can't cope with the limited facilities available. I have these in my <em>~/.gvimrc</em> to use <em>Less</em> and make it behave itself:</p>
<code>
let $PAGER = 'less'
let $LESS = 'dQFe'
</code>
</li>
</ul>
<p>If anybody would like the full paper or slides, please mail me on Smylers@stripey.com and ask.</p>
<div>Smylers</div>
441686
441686