6

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.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85

3 Answers3

14

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

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
12

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
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks! It works! But how can I disable it afterwards? I posted a question, answer there please: [link](https://stackoverflow.com/q/64368210/9391770) – Xopi García Oct 15 '20 at 09:18
3

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

$ vim --cmd "set modeline" yourfile
romainl
  • 186,200
  • 21
  • 280
  • 313