Search code examples
vimvim-pluginpython-mode

(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)


Solution

  • 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.