Most of the time when I edit a text file in vim, I want lines to wrap
automatically as I type, at somewhere around 70 columns wide.
set textwidth=70 in .vimrc.
But sometimes that isn't appropriate. For instance, I have a procmail rules file where I put common spam patterns that programs like spamassassin don't seem to be able to catch. So I might have lines like:
*^Subject:.*(Ink Cartridges|Hummingbird Vine|who's who of executives|Avandia|Botox|Your Email ID|Zoosk|Best airfares on the internet|UGG Boots|police training)... and so on -- you get the idea. I can't have lines breaking in the middle, because then the procmail rule wouldn't work. So every time I add a new phrase, I have to
:set tw=0(or one of the other umpteen ways one can tell vim not to wrap lines) first.
But you can set special behavior for one specific file by adding a special comment called a "modeline" as the first line of the file.
Procmail treats any line starting with a hash, #, as a comment, and vim recognizes # as a comment. So I can add this as the first line of the procmail file:
# vim: set tw=0:then vim will see that and un-set that default text width I specify in .vimrc.
Vim understands most common comment styles, so it should understand lines like
/* vim: set tw=0: */ and
// vim: set tw=0:
; vim: set tw=0: as well.
But to make this work I had to do one more thing: in .vimrc, I had to add
Apparently on some versions of vim this is on by default; in others it's turned off for security reasons (someone could put an evil modeline into a file which would make your vim do something when you edited it). Definitely something to be aware of, but if you mostly edit files you created yourself on your local machine, and no one else uses your machine, it's your choice whether to worry about it.
Emacs has modelines too
Emacs has mode lines too, though it calls them Local variables lines. For instance, C++ files in Mozilla's source tend to start with:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
It's awfully handy to be able to define specific indentation style for the files within a project, making it easy for emacs users, at least, to follow your preferred coding style. If only all editors understood them!
[ 21:26 May 25, 2011 More linux/editors | permalink to this entry | comments ]