I'm editing a Python file that uses two spaces for programmatic indents - I prefer 4 spaces. In my .vimrc I have the following settings related to indentation:
set tabstop=4 "Indentation levels every four columns set expandtab "Convert all tabs typed to spaces set shiftwidth=4 "Indent/outdent by four columns set softtabstop=4
How do I get Vim to convert all the existing 2 space indents to be 4 space indents?
In other words:
if something: dothis()
becomes
if something: dothis()
When I tried gg=G
def check(): for a in list: for b in list2: check(a, b) while (len > MAX) : poll() while(len(thelist) > 0) : poll() return results
became
def check(): for a in list: for b in list2: check(a, b) while (len > MAX) : poll() while(len(thelist) > 0) : poll() return results
Convert all your leading space-tabs to tabs, set the tab size to four, and then convert leading tabs back to space-tabs. Pretty easy to do with a python script too. Just count all the leading spaces, then add the same amount to the beginning of the line and write it back out.
Python doesn't care, as long as you're consistent. So if you start using four spaces for your indent, you always need to use four spaces. For example, in this snippet of code, we can see that the first indent has four spaces, but the second indent has only two. And you can see that the code doesn't line up.
The first line of python code cannot have an indentation. Indentation is mandatory in python to define the blocks of statements. The number of spaces must be uniform in a block of code. It is preferred to use whitespaces instead of tabs to indent in python.
Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation: Change Python (. py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files.
Separate indent from other whitespace. I don't care that tabs can be displayedas different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces.
Python Indentation. Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.
It makes nearly impossible to have inconsistent indentation(I've seen code that normally has 4 spaces indents, but then some parts happen to be one space off, it's difficult to tell by simple inspection if there are 7 or 8 spaces... That wouldn't happen with tabs, unless you set your tabstop to 1 space).
If your indentation is 4 spaces you should never have indentations of less than that. In my opinion the "and" line should be indented one two more levels than the "if" line.
In order to double the number of spaces at the beginning of every line (and only at the beginning):
:%s/^\s*/&&/g
&
in replacement pattern is the matched pattern.
Probably it will not have any side-effect for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With