210

I haven't been able to figure this out yet. Atom seems to use spaces as the default indentation mode. I prefer to have tabs instead though. Sublime Text has built in functionality for switching and converting indentation.

Anyone found out how to change the indentation mode of Atom?

Some screenshots from Sublime Text:

Sublime Text indentation menu open Sublime text indentation menu

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jon Koops
  • 8,801
  • 6
  • 29
  • 51
  • 4
    for converting indentation you can do a regular expression find and replace and use \t to find or insert tabs. – mer10z_tech May 23 '14 at 08:33
  • 1
    is there an easy way to change the current code to 4 from 2 indentation mode? – Charlie Parker Apr 18 '17 at 20:35
  • 1
    I use the auto-detect-indentation plugin, which not only will automatically configure the tab key to do whatever is already in the file, but displays the indentation mode at the bottom left. – while1fork Jun 08 '17 at 15:25
  • @CharlieParker You an go to the preferences, click on the editor tab and set Indentation to 4. – Craig Feb 02 '18 at 05:50
  • **Note:** Atom by default shows and traverses spaces like tabs! See tparker's answer here: https://stackoverflow.com/a/46333677/1599699 – Andrew May 11 '19 at 15:06

17 Answers17

235

See Soft Tabs and Tab Length under Settings > Editor Settings.

To toggle indentation modes quickly you can use Ctrl-Shift-P and search for Editor: Toggle Soft Tabs.

Jon Koops
  • 8,801
  • 6
  • 29
  • 51
blom
  • 3,072
  • 1
  • 15
  • 5
  • 1
    Like @zamber said, if auto indenting a page and it doesn't pick up your indentation settings, re-open it and try again. – Victor S Oct 10 '14 at 13:48
  • 62
    Just as a side note - soft tabs are space characters as tabs, hard tabs are tab characters. Also if the settings are not repspected, try `Ctrl-Shift-P` `Editor: Toggle Soft Tabs` – ivan-k Nov 20 '14 at 04:48
  • 3
    I'd like to add that NONE of the solutions in this thread worked for me UNTIL I swapped to a mono-spaced font. After swapping to a mono-spaced font I tried each method and they all do indeed work. Hope this helps someone in the future... – Native Coder Jan 24 '17 at 02:38
  • 1
    is there an easy way to change the current code to 4 from 2 indentation mode? – Charlie Parker Apr 18 '17 at 20:35
  • I can't see any benefit to NOT using a monospaced font when coding, I must admit. – Frank Bailey May 27 '17 at 07:04
  • 1
    These settings never worked for me on existing files. Having an `.editorconfig` instead worked fine. – killthrush Nov 21 '17 at 18:32
  • If you are looking to convert tab styles (from 4 to 2 or whatever your preference is), select what you want to convert and go to *Edit > Lines > Auto Indent*. – Archonic Jun 30 '18 at 19:03
73

Go to File -> Settings

There are 3 different options here.

  1. Soft Tabs
  2. Tab Length
  3. Tab Type

I did some testing and have come to these conclusions about what each one does.

Soft Tabs - Enabling this means it will use spaces by default (i.e. for new files).

Tab Length - How wide the tab character displays, or how many spaces are inserted for a tab if soft tabs is enabled.

Tab Type - This determines the indentation mode to use for existing files. If you set it to auto it will use the existing indentation (tabs or spaces). If you set it to soft or hard, it will force spaces or tabs regardless of the existing indentation. Best to leave this on auto.

Note: Soft = spaces, hard = tab

brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
28

Add this to your ~/.atom/config.cson

editor:
    tabLength: 4
Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
20

OS X:

  1. Go to Atom -> prefrences or CMD + ,

  2. Scroll down and select "Tab Length" that you prefer.

enter image description here

Said Kholov
  • 2,436
  • 1
  • 15
  • 22
16

You could try going to "Atom > Preferences > Editor" and set Tab length to 4.

enter image description here

This is for mac. For windows you will have to find the appropriate menu.

Craig
  • 620
  • 6
  • 15
7

Adding @Manbroski answer here that worked for me:

try Ctrl-Shift-P Editor: Toggle Soft Tabs

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
7

Late to the party, but a clean way to do this on a per-project basis, is to add a .editorconfig file to the root of the project. Saves you from having to change Atom's settings when you're working on several projects simultaneously.

This is a sample of a very basic setup I'm currently using. Works for Atom, ST, etc...

http://editorconfig.org/

# Automatically add new line to end of all files on save.
[*]
insert_final_newline = true

# 2 space indentation for SASS/CSS
[*.{scss,sass,css}]
indent_style = space
indent_size = 2

# Set all JS to tab => space*2
[js/**.js]
indent_style = space
indent_size = 2
ConorLuddy
  • 2,217
  • 2
  • 19
  • 18
  • I almost threw Atom away entirely till I saw this. config.cson settings appear to do absolutely nothing for existing files. Thanks! – killthrush Nov 21 '17 at 18:31
3

This is built into core: See Settings ⇒ Tab Type and choose auto:

When set to "auto", the editor auto-detects the tab type based on the contents of the buffer (it uses the first leading whitespace on a non-comment line), or uses the value of the Soft Tabs config setting if auto-detection fails.

You may also want to take a look at the Auto Detect Indentation package. From the docs:

Automatically detect indentation of opened files. It looks at each opened file and sets file specific tab settings (hard/soft tabs, tab length) based on the content of the file instead of always using the editor defaults.

You might have atom configured to use 4 spaces for tabs but open a rails project which defaults to 2 spaces. Without this package, you would have to change your tabstop settings globally or risk having inconsistent lead spacing in your files.

Community
  • 1
  • 1
jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • If using 'auto' and it gets it wrong (in my case it's decided to use spaces when I want tabs, on this particular file) how do you (with a quick menu or keystroke, not delving into settings) change its mind? I don't think that auto-detection has failed, it has just got it wrong. It won't type a hard-tab for me. I've done search-and-replace everywhere, but still the tab key inserts 4 spaces. – Ed Randall Feb 15 '17 at 15:46
  • I wouldn't have used `search-and-replace` I would have used the command palette `convert spaces to tabs` (or something like that). I think that has worked for me in the past. – jcuenod Feb 15 '17 at 21:05
3

I just had the same problem, and none of the suggestions above worked. Finally I tried unchecking "Atomic soft tabs" in the Editor Settings menu, which worked.

tparker
  • 689
  • 1
  • 6
  • 17
  • OH! **That's** what that means! I was wondering why my arrow keys were skipping over the spaces when there weren't any `\t`'s in the file... – Andrew May 11 '19 at 15:05
3

If you are using the version 1.21.1:

  1. Click on Packages / Settings View / Open
  2. Select "Editor" on the left side panel
  3. Scrool down until you see "Tab Length"
  4. Edit the value. I like to set it to 4.

Now, just close the active tab pane and you are done.

Caiuby Freitas
  • 279
  • 2
  • 7
3

Tab Control gives nice control in a similar manner to that described in your question.

Also nice, for JavaScript developers, is ESLint Tab Length for using ESLint config.

Or if you're using an .editorconfig for defining project-specific indentation rules, there is EditorConfig

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
2

If you're using Babel you may also want to make sure to update your "Language Babel" package. For me, even though I had the Tab Length set to 2 in my core editor settings, the Same setting in the Language Babel config was overriding it with 4.

Atom -> Preferences -> Packages -> (Search for Babel) -> Grammar -> Tab Length

Make sure the appropriate Grammar, There's "Babel ES6 Javascript Grammar", "language-babel-extension Grammar" as well as "Regular Expression". You probably want to update all of them to be consistent.

Mike M
  • 116
  • 1
  • 4
1

If global tab/spaces indentation settings no longer fit your needs (I.E. you find yourself working with legacy codebases with varied indentation formats, and you need to quickly switch between them, and the auto-detect isn't working) you might try the tab-control plugin, which sort of duplicates the functionality of the menu in your screenshot.

Ed Halferty
  • 136
  • 2
  • 10
1

When Atom auto-indent-detection got it hopelessly wrong and refused to let me type a literal Tab character, I eventually found the 'Force-Tab' extension - which gave me back control. I wanted to keep shift-tab for outdenting, so set ctrl-tab to insert a hard tab. In my keymap I added:

'atom-text-editor': 'ctrl-tab': 'force-tab:insert-actual-tab'

Ed Randall
  • 6,887
  • 2
  • 50
  • 45
1

Changing language-specific configuration

I changed the default tab settings, and it still did not impact when I was editing my files, which were Python files. It also did not change when I modified the "*" setting in ~/.atom/config.cson . I don't have a good explanation for either of those.

However, when I added the following to my config.cson, I was able to change the tab in my Python files to 2 spaces:

'.source.python':
  editor:
    tabLength: 2

Thanks to this resource for the solution: Tab key not respecting tab length

M. K. Hunter
  • 1,778
  • 3
  • 21
  • 27
0

All of the most popular answers on here are all great answers and will turn on spaces for tabs, but they are all missing one thing. How to apply the spaces instead of tabs to existing code.

To do this simply select all the code you want to format, then go to Edit->Lines->Auto Indent and it will fix everything selected.

Alternatively, you can just select all the code you want to format, then use Ctrl Shift P and search for Auto Indent. Just click it in the search results and it will fix everything selected.

ajpieri
  • 306
  • 3
  • 11
0

Yet another answer: If you are using Atom Beautify note that it has its own settings to determine the "Indent Char".

screenshot

ow3n
  • 5,974
  • 4
  • 53
  • 51