9

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)

Community
  • 1
  • 1
songyy
  • 4,323
  • 6
  • 41
  • 63
  • You do know that PEP 8 recommends [4 spaces for indentation](http://legacy.python.org/dev/peps/pep-0008/#indentation)? – gitaarik Apr 03 '14 at 08:19
  • 3
    @rednaw Yeah but I like 2 space.. – songyy Apr 03 '14 at 08:21
  • 1
    @rednaw Hmm a quick rethink probably I should stick to the standard.. – songyy Apr 03 '14 at 08:22
  • The Google coding standards use 2 spaces for indents — in Python and other languages. They're also anal retentive about 80 character maximum line length. You're better off following PEP 8 — unless you're working on Google code. – Jonathan Leffler Jun 19 '18 at 13:56

2 Answers2

3

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • thanks. and how to do if I want to run "let g:pymode_lint = 0" at file startup? – songyy Apr 03 '14 at 08:33
  • You can't do that in modeline - i.e. you can't do it per file. But if you want to disable Python lint globally, just stick that into your `.vimrc`. – Amadan Apr 03 '14 at 08:42
  • 1
    Ah, okay. There is a plugin that would allow that, if you really need it: [let-modeline.vim](https://github.com/vim-scripts/let-modeline.vim). – Amadan Apr 03 '14 at 08:57
  • The PEP8 must NOT be seen as a law or "the only source of good ideas". Code should follow the context defaults for consistency, if any, and that's something also written in PEP8. – H.D. May 13 '14 at 06:56
  • Some PEP8 defaults aren't valid even in the Python standard library itself, such as `TestCase.assertEquals` using lowerCamelCase for the method instead of a `TestCase.assert_equals`. Even consistency is "relative" in some sense, that was done probably due to the XUnit standardization. – H.D. May 13 '14 at 07:35
3

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.

H.D.
  • 4,168
  • 1
  • 18
  • 15