Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Vim) Changing indent setting in Python-mode

I've just installed python-mode, and it has lots of cool features like "syntax-checking".

I like to have 2-spaces of indentation for my python code but the syntax-checking is warning me that it should be 4 spaces.

I believe there should be a variable for me to set this preference. I've read through the docs of pymode, I can't find the related setting.

(Plus, I want to change the shiftwidth setting set by pymode also)

like image 830
songyy Avatar asked Apr 03 '14 08:04

songyy


People also ask

How do I change the indent size in Python?

The size of the tab character is controlled with the Editor > Indentation > Default Tab Size preference.

Does python care about indents?

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to.


2 Answers

The best way to do it is to make a file in .vim/ftplugin/python.vim, and put this in:

setlocal smarttab
setlocal expandtab
setlocal shiftwidth=2
setlocal tabstop=2
setlocal softtabstop=2

This would apply across all of your Python files. Another way is the modeline: put this at the top or at the bottom of all of your Python files to affect only those files:

# vim: sta:et:sw=2:ts=2:sts=2

This requires that you have modeline support switched on, which probably requires you to have set modeline in your .vimrc.

But as rednaw says, it's not exactly a good idea, and if you do do it it's probably best to only do it to the files you make and not all Python files (i.e. leave the ones that are written by other people to be formatted as standard), so I recommend the second method if you really need to do it.

like image 186
Amadan Avatar answered Sep 28 '22 06:09

Amadan


You can have multiple lints. Add this to your ~/.vimrc keeping only the checkers you want.

let g:pymode_lint_checkers = ['mccabe', 'pyflakes', 'pylint', 'pep8', 'pep257']

Trying here with a 2-line indented code, the only messages that appears and are related to the indentation are:

  • [pep8] E111 indentation is not a multiple of four
  • [pylint] W0311 Bad indentation. Found {} spaces, expected 4

These could be easily removed changing the same file:

let g:pymode_lint_ignore = 'E111,W0311'

As well as other messages, if you need. You can also choose to use fewer checkers instead of creating a message code blacklist.

IMHO, style shouldn't be standardized rigidly with annoying checkers. I don't use pylint, pep8 nor pep257, and a configuration like this doesn't need a pymode_lint_ignore, as it was only for pylint and pep8.

like image 38
H.D. Avatar answered Sep 28 '22 06:09

H.D.