Atom - Force Tab Width 2
Asked Answered
S

3

14

I just switched from Sublime Text to Atom in order to turn completely open source.

I have trouble with something very very simple: I want Atom to use always (!) and under any circumstances tab width 2 and replace tab with spaces. This setting is so simple in gedit or Sublime Text, but no matter what I am trying: When I start a new file, tab size is 2 (good!). When I use an existing file, tab size is sometimes 4. I find that a bit annoying.

My current setting in Editor are seen in the screenshot:

enter image description here

Staggs answered 7/2, 2017 at 14:38 Comment(0)
M
14

There is more than one tab setting

Each package (such as python-language) has its own tab setting(s). Whether the language uses the global default or its own default is up to whoever created the package, but you can generally override it.

In your screenshot, you have set the "Tab Type" to "soft". That will take care of using spaces rather than tabs. You have left the default tab width of 2. That is your global setting.

Now, if you look under "Packages" and search for "python" you will find a package named "language-python". Click on its settings button and you will find a number of syntax-specific settings.

  • Python Grammar
  • Python Console Grammar
  • Python Traceback Grammar
  • Regular Expressions (Python) Grammar

Each of those grammars has its own Tab Length setting. You can set them explicitly to 2 here to override the package's default. (You probably mostly care about the first one, Python Grammar.)

Python is different

In the case of Python, the package is explicitly configured to default to 4 spaces, probably because Python is very opinionated about whitespace, and PEP 8 recommends 4-space indents. You can see the default package setting here in the package's source:

https://github.com/atom/language-python/blob/master/settings/language-python.cson

'autoIndentOnPaste': false
'softTabs': true
'tabLength': 4

This overrides the global default. That's why Python Grammar does not honor the global tab width, the way that most packages do.

Sometimes there are package overrides

Additionally, certain packages will override your settings for syntax reasons. For example, language-make will override and use real tabs instead of spaces, because that is required by make.

In the case of Python, there is an override to use spaces. The language-python settings page offers a spot for you to change the indentation level, but it does not offer a way to switch to using tab characters. (That's probably justifiable, as tab characters and mixed indentation in Python are a very common cause of difficult-to-debug syntax errors.)

You might need to reload

Lastly, sometimes settings don't take effect completely until you reload the Atom window. You can use the Window: Reload command to do so. Or using the keyboard:

  • Mac: CtrlOptCmdL
  • Windows/Linux: CtrlAltR
Magneton answered 7/2, 2017 at 17:29 Comment(7)
I tried all these but it doesn't change the number of spaces for a tab.Digitigrade
@soham.m17 All you should need to do is the first part - go into the language-python package settings and change tabs for Python Grammar to 2. The rest of this answer is just further explanation of why Python does not honor the global setting the way most packages do.Magneton
The default in the language-python package is already set to 2. I have clicked the text-box and checked.Digitigrade
@soham.m17 did you do the window reload?Magneton
Yes, I closed and restarted AtomDigitigrade
@soham.m17 I'm not sure what to tell you - I tested the above (changing Python Grammar to 2) on Atom 1.17 on Mac, and it worked. You could try posting on discuss.atom.io, perhaps it can be debugged. Debugging specific user situations is not something stackoverflow is suited for in comments.Magneton
Thank you a lot for this explanation. My tab lengths were set to 2 in the editor settings and I didn't understand why my tabs still stay to a length of 4: the language-C package, but the language-cpp14 package overrode it to 4.Externalism
D
2

This is what worked for me.

  1. Disable all non-default packages
  2. Open ~/.atom/config.cson, and append this (same level than the "*" element)

:

".python.source":
  editor:
    autoIndent: true
    tabLength: 2
  1. Re-enable all packages.

I got this help from someone else. Not my own discovery. However, for confidentiality, I cannot cite the source.

Digitigrade answered 5/7, 2017 at 18:57 Comment(1)
is it possible to force reindentation with just a config change?Path
F
0

Based on soham's answer, I found that setting all tabLength: fields in ~/.atom/config.cson (assuming osx) to your desired length solved the problem.

Flagellant answered 23/6, 2019 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.