I have Vim 7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go.
$ find /usr/share/vim/vim70/ -name \*python\*
/usr/share/vim/vim70/syntax/python.vim
/usr/share/vim/vim70/ftplugin/python.vim
/usr/share/vim/vim70/indent/python.vim
/usr/share/vim/vim70/autoload/pythoncomplete.vim
I would think that when opening a file ending in .py (vim file.py
) it would automatically load these plugins, but I am not sure that is the case. What I want is:
Press TAB and receive four spaces. Auto indent next line for suites, conditionals, etc.
I have this working by explicitly setting tabstop, shiftwidth, etc. in my .vimrc file. Isn't this what the above Python files are for? Why do I have to set these things in my .vimrc
? How do I get these features from the Vim plugins instead?
Current .vimrc:
syntax on
set hls
set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set smarttab
filetype indent on
filetype on
filetype plugin on
Vindect is a Python script to detect the indent options required for editing a Python program. In Vim, output from the :version command should include "+python". The following options are set based upon usage in the current file, and your preferences: shiftwidth , tabstop , softtabstop , smarttab , expandtab .
To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing. If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.
To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).
My understanding is that the python.vim file is just a syntax-highlighting file possibly, because Python files can be indented multiple ways. PEP8 prescribes four spaces, but legacy files could be different including using tabs.
Some of our legacy Python files actually use two spaces per indent. So I leave Python indenting to Vim and configure it per file and per filetype. The following line in .vimrc gives me Python-specific settings which differ from say my xml, xhtml, and html (two spaces).
au FileType python setl shiftwidth=4 tabstop=4
You can also set specific settings by file with a modeline which is handy if you do have legacy files.
# vi: set tabstop=2 expandtab textwidth=70 filetype=python:
Setting tabstop, shiftwidth, etc... in your vimrc is correct. These set your global settings, as well as serve as parameters to the filetype-specific indentation support.
The language indentation plugins use these settings, but typically also set an indent expression (:he inde
) appropriate for the language. Thus the Python indenter should be automatically indenting after a block opening statement (def, class, for...), and dedenting after a closing one (return, pass, continue...) and doing so according to the ts,sw,... you have set.
If you're still unsure if the plugin is loading for a buffer, simply do :filetype
to show the detection, plugin, and indent settings, and :set ft?
to see the detected type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With