14

I'm using Mac OS X 10.12 and I have set noexpandtab in my ~/.vimrc

There are 2 different behaviors :

  1. $ vim file.py

    If run Vim (8.0) to open one file and type :set expandtab?, it says expandtab

  2. $ vim

    If I run Vim by itself and type :set expandtab?, it says noexpandtab

Why is set noexpandtab in my ~/.vimrc ignored when I open a file ?

:verbose set expandtab? says:

expandtab
      Last set from /usr/local/Cellar/vim/8.0.0627/share/vim/vim80/ftplugin/python.vim
SebMa
  • 2,968
  • 2
  • 12
  • 15
  • 3
    You probably have a setting set only for python files. Use :verbose set expandtab? to find out where the setting was last set from. – Marth Sep 08 '17 at 10:00
  • 2
    you probably have a python plugin. Plugins can and will overwrite your vimrc. With Marth's comment you'll find out that this is the case. You can overwrite the settings again, with a file in ~/.vim/after/ftplugin. – B.G. Sep 08 '17 at 10:37
  • @DoktorOSwaldo Do I have to move my set noexpandtab in a file inside .vim/after/ftplugin/ directory ? – SebMa Sep 08 '17 at 12:14
  • yes, you could put it in a file called ~/.vim/after/ftplugin/py.vim or use an autocmd. look here https://stackoverflow.com/questions/28375119/override-options-set-by-ftplugins-in-vim – B.G. Sep 08 '17 at 12:58
  • @DoktorOSwaldo It seems that it works only with a file called ~/.vim/after/ftplugin/python.vim. Can you please update your comment and convert it to an answer ? – SebMa Sep 08 '17 at 15:34
  • 1
    @SebMa ups sorry, of course it is python.vim ... can't edit my comment, but corrected it in my answer. – B.G. Sep 11 '17 at 05:50
  • You can also comment the corresponding line in the plugin file (and add the settings you want). – ado sar Sep 20 '22 at 18:08
  • Though possibly unrelated to this particular OP this question can arise when someone has either deliberately or inadvertently set paste. The paste settings also applies noexpandtab, but expandtab, if originally set, is restored once you set nopaste. – NeilG Aug 18 '23 at 02:07

1 Answers1

13

It is not ignored, it is overwritten by your filetype plugin. In this case the python plugin. You can verify that like Marth said, with the :verbose command:

:verbose set expandtab?

If you want to overwrite the setting in the filetype plugin, you should go for one of the following ways:

  1. Put your settings in a after file: ~/.vim/after/ftplugin/python.vim, see :h after for more information.

  2. Use an autocommand: :autocmd FileType python setlocal noexpandtab, see :h auto for more information. *This must be done after the line which activates the filetype plugins (possibly something like :filetype plugin on)

Note the setlocal, you should use this instead of set in both, the autocmd and your after file. Else you can get it all mixed up if you open different filetypes in the same instance.

DISCLAIMER: Most Lines are directly taken from this SO post: Override options set by ftplugins in vim or the comment of Marth. This answer only adjust them for the given problem. So if this helped you, think about leaving an upvote there!

B.G.
  • 1,116
  • 7
  • 18