Vim: read modeline after opening the file?

1.8k Views Asked by At

I work on a variety of projects and many of them set file-specific vim settings. I have been asked to not have modelines set in .vimrc; is there a way after loading the file to load the modelines settings?

So if I open tmp.c with vim:

int main(int argc, char* argv[]) {
    return 0;
}

/* vim: set expandtab tabstop=4 : */

Is there a command I can run to set the stuff in the modeline? Just doing :set modeline after it is open doesn't do anything.

3

There are 3 best solutions below

0
On

You can tell vim to execute arbitrary commands before sourcing your ~/.vimrc:

$ vim --cmd "set modeline" yourfile
1
On

If you do not want to reload the buffer (e.g. because it contains unpersisted changes, or to avoid clearing the undo history), you can use the fact that a :doautocmd triggers modeline processing:

:set modeline | doautocmd BufRead
0
On

After :set modeline, reload your buffer with :e.