19

My current .vimrc configuration is below:

set nohlsearch
set ai
set bg=dark
set showmatch
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
syntax on
set listchars=tab:>-
set listchars+=trail:.
set ignorecase
set smartcase
map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>

However, when I write python scripts, when I push "ENTER", it will go to the BEGINNING of the next line. What do I add so that it will auto-tab for me?

6 Answers 6

15

Try this:

filetype indent on
filetype on
filetype plugin on

I primarily do Python programming and this is the brunt of my vimrc

set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
filetype indent on
filetype on
filetype plugin on
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I'm using it. Though I also added set expandtab.
12

The short answer is that your autocmd is missing the BufEnter trigger, so it isn't being fired when you create a new file. Try this instead:

 au BufEnter,BufRead *.py setlocal smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

Note that I also changed the set to setlocal. This'll prevent these options from stomping on your other buffers' options.

The "right" way to do what you're trying to do is to add filetype indent on to your .vimrc. This'll turn on the built-in filetype based indentation. Vim comes with Python indentation support. See :help filetype-indent-on for more info.

Comments

7

Consider having a look at the official .vimrc for following PEP 7 & 8 conventions. Present over here

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

Comments

4

You shouldn't have to explicitly indent python keywords. The $VIM/indent/python.vim file takes care of that. You just need to turn on filetype indent and autoindent.

Comments

4

I (simply) use this:

set shiftwidth=4
set tabstop=4
set expandtab
filetype plugin on
filetype indent on
syntax on

Comments

0

I'd say that this configuration provides something, without causing conflicts (/etc/vim/vimrc):

" Python Setup
autocmd Filetype python setlocal ts=2 sw=2 expandtab
autocmd Filetype python set number
autocmd Filetype python set autoindent
autocmd Filetype python set expandtab
autocmd Filetype python set shiftwidth=4
autocmd Filetype python set cursorline
autocmd Filetype python set showmatch
autocmd Filetype python let python_highlight_all = 1
autocmd Filetype python set colorcolumn=80

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.