Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reformat a Python file to have 4 space indentations

Tags:

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 
like image 500
half full Avatar asked Mar 07 '11 07:03

half full


People also ask

How do you add 4 spaces in Python?

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.

Does Python only accept 4 spaces indentation?

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.

How do you indent spaces in Python?

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.

How do I fix spacing 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.

How many spaces do you use for indent in Python?

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.

What is indentation in Python?

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.

Is there such a thing as inconsistent indentation?

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

How many spaces should an indentation be between two lines?

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.


1 Answers

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.

like image 137
Benoit Avatar answered Sep 18 '22 17:09

Benoit