<chunkynotes version="1.0" title="Vim">
   <summary>Assorted vim tips.</summary>

<intro>
<p>Additional contributors: Emil Mikulic, Saied Tahaghoghi.</p>
<p>Note: commands in this document are typed as they would appear in your
.vimrc file. To enter them as commands in an active vim session prefix them
with a colon. For example:</p>
<sample><src>:set cindent</src></sample>
</intro>

<chunk title="Matching brackets and braces">
<p>When the cursor is over one of a pair of parentheses, square brackets or
braces, press % to move quickly to the other. You can also use the % key as a
destination argument to many vi commands (e.g. d% will delete everything
between a pair of braces, including the braces).</p>
<p><src>set showmatch</src> will briefly highlight a matching
brace/parenthesis/square bracket when you type it's partner.</p>
</chunk>

<chunk title="Automatic indenting, Smart Indenting and C-indenting">
<p>You can turn "dumb" automatic indenting on with <src>set ai</src> (or
autoindent) and off with <src>set noai</src> (or noautoindent). This just
starts a new line at an indent equal to the current one.</p>

<p>You can enable smart indenting with <src>set si</src> - amongst other
things this will automatically adjust indenting for braces and push hash
comments to the first column. This second "feature" can be annoying but is
easily disabled using <src>:inoremap # X^H#</src> (use ctrl-v ctrl-h to enter
the ^H).</p>

<p>Vim also has a "cindent" mode that is very useful for c-like code and is
highly configurable - see the help file indent.txt for details. You can try it
out with <src>set cindent</src>.</p>
</chunk>

<chunk title="Auto-indenting and X Pasting">
<p>Automatic indenting makes X copy-and-paste untenable (try it and see!) so
it's useful to have a key which will enable/disable it for pasting. You can
set this with pastetoggle - <src>set pastetoggle=&lt;F11&gt;</src>.</p>

<p>Alternatively, enter the command <src>:a</src> (ex-style append), paste
your text and press ctrl-C. This will enter text unmodified by any
indenting abbreviation or similar commands. Thanks to Emil for this tip.</p>

<p>If you want to reindent a chunk of text (e.g. you've just pasted in some
badly formatted code) by selecting it in insert mode and pressing =. Thanks to
Saied for this tip.</p>
</chunk>

<chunk title="Abbreviations">
<p>Use <src>ab [abbreviation] [expansion]</src> to define an abbreviation,
where [abbreviation] is a single word (it can expand to many words). Typing in
the abbreviation followed by a space will make vim automatically expand the
text. For example:</p>
<sample> <src> ab uns unsigned short </src> </sample>
</chunk>

<chunk title="Highlighing trailing whitespace and tabs">

<p>It's useful to highlight "trailing" whitespace - spaces and tabs at the end
of the line. These don't display anything but can mess up automatic wrapping,
version control software (like RCS, CVS and Subversion), diffs... The
following commands, thanks to Emil, will display trailing whitespace in dark
grey:</p>
<sample>
<src>
set list listchars=trail:.
highlight SpecialKey ctermfg=DarkGray
</src>
</sample>
You can also display tabs (so you can differentiate them from spaces) and
other character classes:
<sample>
<src>
set list listchars=tab:\|_,trail:.
</src>
</sample>
</chunk>

<chunk title="Mapping a filter command to a key">
<p>You can easily set up a key to automatically pipe the editor contents
through a command:</p>
<sample><src>map x :%!somecommand -arg1 -arg2&lt;CR&gt;</src></sample>
<p>The &lt;CR&gt; simulates a carriage return keypress (otherwise the mapping
would just print the command on the : line). This is useful for rot13 scripts.</p>
</chunk>

<chunk title="Modelines - including vim settings in an edited file">

<p>A modeline is a line close to the beginning or end of a file which
contains vim settings. When the file is opened for editing vim will read the
modeline and use the given settings.  Modelines take one of two forms:</p>

<sample>
<src>
   vim: shiftwidth=3 tabstop=3 expandtab
   vim: set shiftwidth=3 tabstop=3 expandtab:
</src>
</sample>
<p>You can place the modelines inside a comment but they need whitespace
before and after or they will not function. Modelines at the beginning of a
line will be ignored in some circumstances - try indenting them slightly.</p>

</chunk>

<chunk title="File type dependent settings">
<p>The simple way to do this is to use an autocommand with the file extension:</p>
<sample>
<src>
   au BufRead,BufNewFile *.c     set tabstop=3 | set shiftwidth=3
</src>
</sample>
<p>Another way is using the builtin Vim filetype detection. This requires the
filetype plugin installed, but has the advantage that it can detect files
based on their contents as well as the extension - and doesn't require you to
write your own detection rules. Setting the same values as above:</p>
<sample>
<src>
   au BufRead,BufNewFile *    if &amp;ft == 'c' | set ts=3 | set sw=3 | endif
</src>
</sample>
<p>The 'c' filetype is autodetected by the filetype plugin which sets the
filetype/ft variable to 'c'. Check "filetype.vim" on your system to see which
filetypes are available.</p>
</chunk>

<chunk title="Creating your own filetypes">
...
</chunk>

</chunkynotes>
