The “modeline” is a common way to set (or override) VIM settings on a file by file basis. Let’s see a few of the key concepts of the VIM modeline.
A useful example is to set the textwidth to 78 when typing in a simple text file, so that lines will automatically be broken at words limits when the line gets longer than that. You obviously don’t want to do that for source files, and probably not neither for every text files.
You could manually set the textwidth everytime you open that one file by typing
But that would soon become annoying. Besides you’ll probably want to set a couple of other parameters for that specific file (such as tabstop to 4 for example).
An easy way to do that is to set a modeline for the file. The modeline is actually the last line of the file and should read like
Here is what you’d do for the textwidth + tabstop example :
The optional leading [text] is so that you’d put the modeline in a comment, so that it would be ignored by an eventual processing tool (compiler, script engine, etc), like such for a shell script :
# vim: ts=8:sw=8:
or for a C source file :
/* vim: ts=4:sw=4: */
a little trouble shooting note : if your modeline doesn’t seem to apply, beware that it is only enabled by default when in nocompatible mode. That is you need to have the following line in your .vimrc file :
One thought on “VIM: using the modeline for file based customized editing parameters”
Comments are closed.