Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atom - Force Tab Width 2

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

like image 613
MenschMarcus Avatar asked Feb 07 '17 14:02

MenschMarcus


People also ask

How do you set the tab width of an Atom?

Go to Atom -> prefrences or CMD + , Scroll down and select "Tab Length" that you prefer.

How do you indent in an Atom?

Highlight the lines of code you want to re-indent, and activate the re-indent command either through the command palette ( ctrl-shift-p ) or by using the keybinding ctrl-shift-r .


1 Answers

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
like image 181
Dan Lowe Avatar answered Oct 19 '22 09:10

Dan Lowe