Why does vim not obey my expandtab in python files?

后端 未结 2 805
余生分开走
余生分开走 2020-11-29 04:48

After I installed Vundle, my vim no longer obeys the expandtab settings I had. My tabs were set to 2 spaces, but now in python files it no longer does that. The problem is b

相关标签:
2条回答
  • 2020-11-29 05:20

    Overridden settings

    It could be that the settings are being overridden by language-specific settings. See http://vim.wikia.com/wiki/Keep_your_vimrc_file_clean for more information:

    The quick way to get started is to move all the language-specific stuff from your .vimrc file into a file named .vim/ftplugin/language.vim (or $HOME/vimfiles/ftplugin/language.vim on Windows).

    Check in those locations for a python specific .vim file.

    Filetype on

    Vundle appears to require filetype off, and I'm not sure if you should turn it back on. There's a thread on the github issues page for Vundle explaining why filetype on is required. Perhaps this will provide some insight.

    I also think having filetype plugin indent on followed by filetype on is redundent. According to the vim help docs, the former turns detection, plugin and indent on, and the latter turn detection on and leaves the plugin and indent unchanged:

    Overview:                   *:filetype-overview*
    
    command                     detection       plugin          indent
    
    :filetype on                on              unchanged       unchanged
    :filetype off               off             unchanged       unchanged
    :filetype plugin on         on              on              unchanged
    :filetype plugin off        unchanged       off             unchanged
    :filetype indent on         on              unchanged       on
    :filetype indent off        unchanged       unchanged       off
    :filetype plugin indent on  on              on              on
    :filetype plugin indent off unchanged       off             off
    
    0 讨论(0)
  • 2020-11-29 05:25

    The problem is that your settings are being overridden by a filetype plugin that's part of Vim. The issue is in ftplugin/python.vim:

    " As suggested by PEP8.
    setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
    

    The python plugin attempts to setup your source code to be PEP8 compliant by default, so it's adjusting the tabstop. You'll want some of what these plugins have to offer, but you may need to setup your own autocommands to fixup anything you don't like.

    There are two ways to go about doing this. If you have a ~/.vim folder, the easiest way is to add the file ~/.vim/after/ftplugin/python.vim:

    " Here, you can set the setting directly, or call a command or function
    " to help you.  We'll call a command, and then implement that command in
    " your top-level vimrc to help keep things in one place.
    SetupPython
    

    In your .vimrc, add:

    function! SetupPython()
        " Here, you can have the final say on what is set.  So
        " fixup any settings you don't like.
        setlocal softtabstop=2
        setlocal tabstop=2
        setlocal shiftwidth=2
    endfunction
    command! -bar SetupPython call SetupPython()
    

    The latter bit just allows you to call the function as SetupPython rather than call SetupPython() in the after file.

    The other way, is to keep everything in your .vimrc, but you use the VimEnter autocommand to setup a FileType autocommand for python to set your preferences. By waiting until VimEnter is triggered, all the other plugins will have had time to setup their autocommands, so your's will be added to the end of the list. This allows you to run after the python plugin's FileType autocommand and set your own settings. This is a bit of a mess though, and the after/ mechanism above is the preferred way of doing this.

    FWIW, many common settings I keep in a SetupSource() function to be called from a number of different FileTypes. Then I'd have SetupPython() call SetupSource(). This helps to keep the specific functions a little cleaner and reduce some duplication. If it helps, take a look at the functions in my vimfiles here: https://github.com/jszakmeister/vimfiles/blob/master/vimrc#L5328

    0 讨论(0)
提交回复
热议问题