4

In my .vimrc file I have this line..

filetype plugin indent on

and then in ~/.vim/ftplugin/py.vimrc (I've also tried saving it as a .vim file) I've copied and pasted a vimrc file for python standards and have enabled all of the suggested settings in it (uncommented them). When I create and open a test.py file and do something like a tab, it goes 8 spaces instead of 4.

Not sure what's going wrong.

3 Answers 3

1

I'd replace

au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
fu Select_c_style()
    if search('^\t', 'n', 150)
        set shiftwidth=8
        set noexpandtab
    el 
        set shiftwidth=4
        set expandtab
    en
endf

with

" indenting
set cindent
set autoindent
set shiftwidth=4
set softtabstop=4
set tabstop=4
set expandtab
set backspace=indent,eol,start

because it is not clear that either the Select_c_style() function is getting called or that the quoted code is enough.

Yes, my second .vimrc extract pretty much prevents you from using tabs anywhere without an explicit Ctrl-V Tab, but literal tabs in most Unixy situations are just an irritant anyway. (and Pythonistas who post .vimrc files with vim abbreviations should be forced to use notepad, but that's a different issue ;)

3
0

The short answer is that you should put that file in your ~/.vim/plugin directory rather than in your ~/.vim/ftplugin directory. If you do that, it won't matter what the base name of the file is, but the suffix must be ".vim".

According to the header of your file, it can be sourced at any time and will not affect any files other than Python files. So it is safe to put the file in your ~/.vim/plugin directory where it will be sourced every time you start Vim.

There are three reasons putting py.vimrc in your ~/.vim/ftplugin directory does not work. The first is that the suffix must be ".vim". The second is that the base name of the file must be the same as Vim's name for the file type. For Python, the file type is "python". Therefore, the name of a filetype plugin for Python must be "python.vim". The third reason is that the autocommands in that file are read when the file is sourced, which would be when you open a Python source file (if everything else was correct). Their commands are then ready to be executed the next time you open a Python file.

Lastly, don't change the 'tabstop' setting to 4. Even if you use an indentation of 4, set 'shiftwidth' to 4 and maybe 'softtabstop' to 4, but leave 'tabstop' at 8. The reason is that the last time I checked, the Python interpreter assumes tab stops of 8. If you're careful and consistent, you can get away with using other 'tabstop' values, but I think it's risky.

2
  • You're right about ftplugin naming policies and about global changes (the OP is using :set instead of :setlocal). However, as these settings are meant for python editing, they shall be inside a ftplugin. Jun 16, 2010 at 6:06
  • If he uses that file unchanged, it can't be an ftplugin. As an example, assume he does use that file as an ftplugin, then edits some file foo.py. Vim will generate a BufRead event which will trigger the autocommand in $VIMRUNTIME/filetype.vim matching *.py which will then generate a FileType event for "python" and source his file. The second command of that file is "au BufRead,BufNewFile *.py,*pyw set shiftwidth=4". That will enable the autocommand but not execute the "set shiftwidth". The "set shiftwidth" will not be executed until he loads a second python file.
    – garyjohn
    Jun 16, 2010 at 15:35
-1

Are you setting filetype plugin on? You might need to do that rather than just filetype plugin indent on.

2
  • filetype plugin indent on implies filetype plugin on Jun 16, 2010 at 6:00
  • ah, here it is, :help :filetype-overview
    – intuited
    Jun 16, 2010 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .