Kvass
Kvass

Reputation: 8434

Vim - ftplugin settings changes not updating for existing file

It seems like there's some odd caching-like behavior going on for files that I have already opened once in Vim. I have a file foo.txt that I opened, and then I change some of the settings in ~/.vim/after/ftplugin/text.vim, but those new settings do not appear in foo.txt. I can quit vim and reopen foo.txt, or reload with :e, or even :so ~/.vim/after/ftplugin/text.vim, but none of these seem to have an effect on foo.txt's settings. If I mv foo.txt bar.txt, the settings show up for bar.txt with no issues.

EDIT

It seems I can force the settings to reload for foo.txt with the following sequence:

  1. :so ~/.vimrc
  2. :so ~/.vim/after/ftplugin/text.vim

Questions:

  1. Why is this necessary / why were the other settings not picked up?
  2. Why was sourcing ~/.vimrc not enough? It applied settings that were directly specified in ~/.vimrc, but shouldn't the ftplugins have been loaded at the line filetype plugin indent on? Why was sourcing text.vim afterwards necessary?

Upvotes: 1

Views: 811

Answers (2)

Luc Hermitte
Luc Hermitte

Reputation: 32946

A few more precisions:

  • .vimrc is loaded once -> for defining global stuff
  • same thing with .gvimrc, loaded only with gvim after the .vimrc
  • plugins are loaded once as well, after the vimrc files -> for global stuff as well
  • autoloaded plugins are loaded on demand, once, whenever a function they define is invoked
  • ftplugins are loaded once per buffer (and possibly multiple times! as there may be multiple buffers requiring them to be sourced) -> for definitions that shall apply only to the current buffer that triggered its loading
  • there are also langmap scripts, syntax scripts, indentation scripts, and even the old macros/ scripts (loaded explicitly/manually)
  • some plugins provides local vimrcs (basically identical to ftplugins, but loaded depending on the current directory of a buffer, instead of its filetype).

As you see, all things are loaded once, only. If you want to load them several times, you'll have to do it manually and explicitly. That's why a few of us have a :Reload command that simplifies reloading any kind of script - mine is hidden in my collection of scripts: lh-misc -> plugin/vim-maintain.vim

Upvotes: 1

Sato Katsura
Sato Katsura

Reputation: 3086

Vim runtime consists of a few hundred files, I'm not sure why you expect Vim to monitor them continuously. It doesn't. These files are loaded at some well-defined (and documented) points, that's all there is to it.

In particular there is no safe way to reload your configuration. You can do things like :so ~/.vimrc, but unless you specifically wrote your vimrc to take that into account, there will be drawbacks (such as autocmds piling up). If you want to be safe you have to quit Vim and start it again. That's how Vim works.

Now, for ftplugins you might get away with something like this:

:setf text

(use the actual filetype instead of text). This works for simple set fubar options. It works because under the hood setf is actually a carefully written autocmd. It still breaks for more complicated constructs (such as autocmds or file-scoped variables), for the same reasons :so ~/.vimrc has drawbacks.

Upvotes: 2

Related Questions