9

I have encountered something that I have never seen before in Vim. While I was tentatively modifying some of the source code for software I've been planning to work on, I noticed vim was displaying incorrect spacing for the line of code that I added. Gedit shows the code with the correct spacing. I know Gedit has the correct spacing because the Python code delivers errors if I change the code in Vim to be where it appears to belong within the Vim window. I have attached the Images below. The line that I added is the line that says: print "I am about to evaluate a factorial". I have included my short .vimrc file as well. Has anyone seen this? I have been using vim for months now and can't recall anything like this ever occuring.

  • Vim (Incorrect):

    Vim wrong

  • Gedit (Correct):

    Gedit right

  • Vimrc:

    Vimrc file

3
  • 3
    I know this might be a bit of a hassle, but could you turn on set list listchars=tab:>- and re-upload the first (Vim) picture? I think it would help us see what's going on with the tabs.
    – Tumbler41
    Jun 22, 2016 at 21:50
  • It looks to me like your vimrc is working just fine. The tab is 4 spaces as desired. However, the rest of the document is using only spaces. I would just use spaces on this line to fix the issue. Or at any rate, stay consistent between using tabs and spaces for indentation.
    – Tumbler41
    Jun 22, 2016 at 21:59
  • Using all spaces worked, thank you. I am surprised however as I would expect the tab to not be interpreted differently.
    – BenB
    Jun 22, 2016 at 22:03

3 Answers 3

17

It gets reset by the Python filetype plugin; from /usr/share/vim/vim74/ftplugin/python.vim:

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

This file is loaded every time a Python file is loaded.

To override it add this in your vimrc:

augroup python
    autocmd!
    " Add shiftwidth and/or softtabstop if you want to override those too.
    autocmd FileType python setlocal noexpandtab tabstop=4
augroup end

This will get loaded after the ftplugin file, overriding the settings from there.

I would recommend against using tabs in Python files, as the community standard is to use spaces.

1
  • Yeah so make sure you have filetype plugin on in your vimrc. I wouldn't override the set tabstop=8 though. The reason is here
    – Antony
    Jun 22, 2016 at 22:28
3

Solution for me was to add this line after the filetype plugin on into my ~/.vimrc file

filetype plugin on
autocmd FileType python setlocal noexpandtab shiftwidth=4 softtabstop=4
1

Because the file python.vim, path in Debian 9 /usr/share/vim/vim81/ftplugin/python.vim, contains the following code

if !exists("g:python_recommended_style") || g:python_recommended_style != 0
    " As suggested by PEP8.
    setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
endif

the best to turn it off is to put

let g:python_recommended_style=0

into .vimrc and the code would not execute at all, because the plugin is usually executed after .vimrc.

Another solution is to create ~/.vim/after/ftplugin/python.vim hook as suggested in https://vi.stackexchange.com/a/13538/41302

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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