0
$ vim --version
VIM - Vi IMproved 8.1

I set up the following vim configuration files:

" .vim/ftplugin/text.vim

set textwidth = 0
" .vim/ftplugin/python.vim

set textwidth = 99

set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab

Now I do the following:

  1. Open a text file with vim from the terminal, vim text.txt.
  2. Enter insert mode and type a tab.
  3. Exit to normal mode and open a python file in a split window :sp python.py.
  4. Enter insert mode and type a tab.

The result is a long tab in the text file and 4 spaces in the python file, as expected. When I switch between the windows and keep inserting tabs, the text file keeps getting long tabs and the python file keeps getting sets of 4 spaces, as expected.

However, when I do the following:

  1. Open a python file with vim from the terminal, vim python.py.
  2. Enter insert mode and type a tab.
  3. Exit to normal mode and open a text file in a split window :sp text.txt.
  4. Enter insert mode and type a tab.

Both files get a set of 4 spaces. Switching between the windows and inserting more tabs keeps inserting more sets of 4 spaces in both files.

My problem is that vim's behavior is dependent on the order in which I open my files. I realize that I can fix this by explicitly specifying my preferred tab behavior in .vim/ftplugin/text.vim. However, I'd like to use more file types and vim options than the above, and some vim options are only relevant for specific file types, so I don't want to mirror any vim setting I set for one file type across all the files in ftplugin/. Is there a way to tell vim that it should revert to it's default behavior for any window's file type (here text) for any options that are not explicitly set in that file type's ftplugin/ (here the tab options)?

1 Answer 1

1

The :set command sets both local and global option values. So after your ftplugin was applied it also affects all new buffers as "the new default".

Therefore one normally does :setlocal and never :set in ftpligins.

2
  • This answer is correct, but be aware that some settings cannot be set local to the buffer. The :help 'setting' page for each setting will tell you if it's a global setting (like 'timeoutlen') or can be local to a buffer. Nov 4, 2021 at 15:16
  • 1
    @ChrisHeithoff Yes, but (a) Vim is forgiving, so :setlocal works for global-only settings too; (b) touching globals within ftplugin is a bad style anyway.
    – Matt
    Nov 4, 2021 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.