How do I set up Vim autoindentation properly for editing Python files?
Asked Answered
U

8

96

I've trouble setting up Vim (7.1.xxx) for editing Python files (*.py). Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.

Urquhart answered 15/9, 2008 at 17:48 Comment(3)
What exactly is your problem? How is the indenting broken?Borchert
What platform are you using? Windows/Mac/Linux?Extrusion
Add this to .vimrc filetype plugin indent onFecteau
A
86

I use this on my macbook:

" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler               " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line

(edited to only show stuff related to indent / tabs)

Agrapha answered 15/9, 2008 at 17:53 Comment(3)
Don't use tabs when editing C-style languages. s/noexpandtab/expandtabPurview
@AlexKreimer you're probably right - I wrote this in 2008 - that's a long time ago. I'd love to update it, but I've moved on from using vim for most stuff. Be sure to come back here and post a link to a better answer (or write one yourself) when you figure out a better solution!Agrapha
@DarenThomas IMO, a very outdated answerSaxon
S
15

I use:

$ cat ~/.vimrc
syntax on
set showmatch
set ts=4
set sts=4
set sw=4
set autoindent
set smartindent
set smarttab
set expandtab
set number

But but I'm going to try Daren's entries

Semiconductor answered 6/8, 2012 at 15:1 Comment(1)
Notice that smartindent is only suitable for editing C files, not Python files (and anyway has been deprecated by now; see https://mcmap.net/q/45278/-tab-key-4-spaces-and-auto-indent-after-curly-braces-in-vim).Fujimoto
S
15

A simpler option: just uncomment the following part of the configuration (which is originally commented out) in the /etc/vim/vimrc file:

    if has("autocmd")
      filetype plugin indent on
    endif
Swirl answered 3/6, 2017 at 2:5 Comment(0)
I
5

I use the vimrc in the python repo among other things:

http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc

I also add

set softtabstop=4

I have my old config here that I'm updating

Invincible answered 8/12, 2009 at 18:37 Comment(0)
E
3

Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.

In vim type

:help vimrc

and check your path to the _vimrc/.vimrc file with

:echo $HOME

:echo $VIM

Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file.

:help source

Extrusion answered 15/9, 2008 at 20:50 Comment(0)
S
1

Combining the solutions proposed by Daren and Thanos we have a good .vimrc file.

-----
" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set noexpandtab
au BufRead,BufNewFile *.h set noexpandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line
set smartindent
set smarttab
set expandtab
set number

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler                           " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line
Skepful answered 13/2, 2020 at 10:25 Comment(0)
A
0

for more advanced python editing consider installing the simplefold vim plugin. it allows you do advanced code folding using regular expressions. i use it to fold my class and method definitions for faster editing.

Antiseptic answered 15/9, 2008 at 23:42 Comment(0)
M
0

I added this line in my vimrc , the difference of the other responses is the noexpandtab, this way , the TAB is just a TAB not spaces, it made the difference

autocmd FileType python setlocal tabstop=8 softtabstop=8 shiftwidth=8  autoindent noexpandtab
Maurizia answered 8/2, 2024 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.