For vim I use a .vimrc that has taken a couple of years to be happy with. For example it sets tabs to be replaced by four spaces. However, there are files for which that is a bad idea. Makefiles won’t work unless the leading whitespace is a tab, and js and css files will be leaner if one tab is used instead of four spaces. I could make my .vimrc smart enough to do that, but that wouldn’t help others who might edit these files without the advantage of my .vimrc. Modelines solve the tabs vs spaces problem.

To set my main.css file so that tabs don’t get converted to spaces but still appear equivalent to four spaces, I can put this line at the end of the file.

// vim: ai:ts=4:sw=4:et!

I have to enable interpretation of modelines, especially if I want them to work for ‘root’.

set modeline
set modelines=2

which instructs vim to look at the first and last two lines to check for a modeline.
For my main.css example I could have used parenthesis style comments /* ... */ but these require use of set which in turn requires a different use of colons. This time interpretation terminates after the first colon following set

/* vim: set ai ts=4 sw=4 et!: */

This won’t change existing spaces, for which I might want to try

:%s/    /^I/g

where “^I” is actually <ctrl>+<v>,<tab>
Thus file-by-file I can choose whether real tabs should be used instead of spaces, while keeping spaces as my overall default.

Advertisements