Notes - Vim

.plan
[Last 5] [Last 10] [Index] [RSS Feed]
[Full .plan (over 1MB)]
Contact
links

Notes

Notes
A collection of notes on various topics, such as version control, programming, food and email/newsgroup usage.
SubVersion
Vim
C and C++
FreeBSD
more...

Image Gallery

Image Gallery
Photos
Screenshots
more...

RMITCS

RMITCS Fluxbox Menu Generator
C Helpdesk Resources
Customising Your CS Account [outdated]
A brief FAQ/HOWTO on Customising X, tcsh and vim at RMITCS.

Software

Codepile
Assorted little scripts and apps.
dumps.pl - FreeBSD Backup script
dice.pl - Perl Dice roller, supports Silhouette, Shadowrun and Alternity
rastodo.py - Python Console todo/reminder
timetable.c - Console timetable/reminder
more...
Lost Emulators
Mirrors of some abandoned emulators (Generator-cbiere and DGen-SDL).
NumLock
An abandoned client-server numbers game.

Videogame Mods

LethalMod
A Max Payne 2 mod that changes the gameplay to make combat more realistic and deadly.
terroristgear
[1.4 KB .zip] A little Rogue Spear: Urban Operations mod that lets you use terrorist and multiplayer equipment (such as C4) in single player games.

Roleplaying Games

Shadowrun (4th ed)
My house firearms rules, notes on armour concealability and simplified matrix rules.
Heavy Gear (2nd ed)
A new and improved character sheet, notes on tool kits and links.
GURPS
A quad NPC sheet, firearms malfunctions and links.
GameMastering
Assorted tips for gamemasters (not specific to any particular game).

Miscellaneous

Taglines
Quotes and stuff.
crazy
Images as preformatted text, with PHP source.
Trombone slide position chart
PDF, 14KB, one a4 page.
[Back to home page]
[Back to Index]

[Raw XML]
Assorted vim tips.

Contents:

Additional contributors: Emil Mikulic, Saied Tahaghoghi.

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:

:set cindent

Matching brackets and braces

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).

set showmatch will briefly highlight a matching brace/parenthesis/square bracket when you type it's partner.

Automatic indenting, Smart Indenting and C-indenting

You can turn "dumb" automatic indenting on with set ai (or autoindent) and off with set noai (or noautoindent). This just starts a new line at an indent equal to the current one.

You can enable smart indenting with set si - 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 :inoremap # X^H# (use ctrl-v ctrl-h to enter the ^H).

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 set cindent.

Auto-indenting and X Pasting

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 - set pastetoggle=<F11>.

Alternatively, enter the command :a (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.

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.

Abbreviations

Use ab [abbreviation] [expansion] 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:

  ab uns unsigned short  

Highlighing trailing whitespace and tabs

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:


set list listchars=trail:.
highlight SpecialKey ctermfg=DarkGray

You can also display tabs (so you can differentiate them from spaces) and other character classes:

set list listchars=tab:\|_,trail:.

Mapping a filter command to a key

You can easily set up a key to automatically pipe the editor contents through a command:

map x :%!somecommand -arg1 -arg2<CR>

The <CR> simulates a carriage return keypress (otherwise the mapping would just print the command on the : line). This is useful for rot13 scripts.

Modelines - including vim settings in an edited file

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:


   vim: shiftwidth=3 tabstop=3 expandtab
   vim: set shiftwidth=3 tabstop=3 expandtab:

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.

File type dependent settings

The simple way to do this is to use an autocommand with the file extension:


   au BufRead,BufNewFile *.c     set tabstop=3 | set shiftwidth=3

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:


   au BufRead,BufNewFile *    if &ft == 'c' | set ts=3 | set sw=3 | endif

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.

Creating your own filetypes

...
Generated Fri, 18 Jun 2010 14:53:06 +1000
Copyright © 2002-2010 Dylan Leigh.
[HTML 4.01 Transitional]