4

Is there a way to indent automatcally after typing a : when editing python in vim? I works that way in CodeMirror and I found it nice; so I wondered if vim has that capability?

1 Answer 1

7

Try adding the following lines to your .vimrc:

syntax on
filetype plugin indent on

N.B. The plugin is not necessary for indenting, but it enables filetype plugins to run, which you probably want.

This will cause Vim to load its Python indent script when you edit a Python file, which will set the following option:

:set indentexpr=GetPythonIndent(v:lnum)

This will cause Vim to use an automatic indent on lines after statements that end in : characters.

If you'd like to see how this works, you can view the indent script with the command:

:e $VIMRUNTIME/indent/python.vim

See :h :filetype-indent-on and :h indent-expressions for more details.

If you find Vim's included indent file insufficient for your needs, you might like to try this plugin, which offers a few refinements over the built-in version: this is what I'm currently using for Python indenting.

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.