Space alignment


#1

Not sure how best to describe this, but if I typed the following into atom

x = call_function(x=5, y=7,
                  z=8)

When I broke the line to put z=8 on the new line I’d like to be able to tab (or emulate tab with spaces) so that the x and z were in alignment. I can do this fine in my normal text editor but every time I try (even when using spaces), it appears fine within the text editor but if you view the text editors from a shell prompt (e.g. more test.py) the alignment is incorrect, e.g.

x = call_function(x=5, y=7,
                                      z=8)

thanks,

Martin


#2

Looks like the tab width. Old-school Unix programs assume that a tab is eight characters wide, and they mixed tabs and spaces to achieve the desired output. New-school programs allow you to configure the tab width, and then they just use tabs.

It looks as if your tab width was set to 4 but the more program that you used assumed a tab width of 8.

I think Atom has a setting that allows you to indent using spaces, not tabs.

Theoretically, the new-school approach has advantages, but you have to distinguish between indentation (used inside an if or a loop) and alignment (used to move the beginning of the current line to a specific spot in the previous line). Consider indentation:

if some_condition:
    some_statement
    another_statement

If you use one tab character to indent the body of the if statement, then you can select a tab width of 4 and I can select a tab width of 8, and we can both edit the same file even though we prefer different indentation levels.

But of course, this does not work for alignment – in your example, the number of spaces at the beginning of the second line is exactly determined by the number of characters x = call_function(.

It’s theoretically possible to cater for both: use tabs for indentation, then follow with spaces for alignment, if alignment is desired. It’s harder to figure out which part of the line is indented and which part is aligned, but it’s theoretically possible.


#3

I’m not sure my other text editor is doing anything particular fancy, as I understand it textwrangler is just emulating tabs with spaces. This should of course result in perfect alignment as the amount of spaces you use within the text editor will match what you see when you look at a script outside. I’m not sure I follow why this would be different for atom. If it also emulates tabs with spaces you should get the same result?


#4

Atom emulates tabs with spaces if you have the “Soft Tabs” setting enabled:

Though, there are some issues with how Soft Tabs is implemented currently and they are being collected here:

You can also see what Atom is inserting into your text file if you enable the “Show Invisibles” setting.


#5

Soft tabs is turned on and the tab length is set to 4 as it is in my other text editor…