242

Example: If I have a document with 2 space indentation, and I want it to have 4 space indentation, how do I automatically convert it by using the Sublime Text editor?

Magne
  • 16,401
  • 10
  • 68
  • 88

11 Answers11

560

Here's a neat trick in Sublime Text 2 or 3 to convert your indentation spacing in a document.

TL;DR:

Converting from 2 spaces to 4 spaces:

Ensure tab width is set to 2. Convert your 2-space indentation to tabs, switch to tab width 4, and then convert the indentation back to spaces.

The detailed description:

Go to:

  • View -> Indentation

It should read:

  • Indent using spaces [x]
  • Tab width: 2

Select:

  • Convert Indentation to Tabs

Then Select:

  • Tab width: 4
  • Convert Indentation to Spaces

Done.

Magne
  • 16,401
  • 10
  • 68
  • 88
  • 5
    Thank you! :D Saved a lot of time. Note. You can also do "find: space-space => select all => tab" – Automatico May 04 '13 at 21:11
  • 11
    I created a Macro for sublime that does all of this for you https://gist.github.com/joshmfrankel/5707020. Enjoy :-) – Josh Frankel Jun 04 '13 at 15:53
  • 3
    I want to point out that the "Convert Indentation to Spaces" also works if you have a text file where the tabs are not taking up the whole tab-width. Say, for a file where the author used tabs to format columns, so if you were to do a find and replace on the tabs, the resulting columns would no longer be lined up. Using "Convert Indentation to Spaces" will keep it lined up correctly though – Joe Pinsonault Dec 04 '13 at 18:14
  • I also created macros and example keybindings for this for a coworker before I saw Josh's macro. Mine uses ctrl+2 and ctrl+4 to allow switching back and forth: https://gist.github.com/beaugunderson/8588871 – Beau Jan 23 '14 at 23:25
75

I actually found it's better for my sanity to have user preferences to be defined like so:

"translate_tabs_to_spaces": true,
"tab_size": 2,
"indent_to_bracket": true,
"detect_indentation": false

The detect_indentation: false is especially important, as it forces Sublime to honor these settings in every file, as opposed to the View -> Indentation settings.

If you want to get fancy, you can also define a keyboard shortcut to automatically re-indent your code (YMMV) by pasting the following in Sublime -> Preferences -> Key Binding - User:

[
  { "keys": ["ctrl+i"], "command": "reindent" }
]

and to visualize the whitespace:

"indent_guide_options": ["draw_active"],
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true,
"draw_white_space": "all",
"rulers": [120],
Brian Gerstle
  • 3,643
  • 3
  • 22
  • 22
10

I found, in my mind, a simpler solution than Magne:

On mac:

"cmd+f" => "  "(two spaces) => "alt+enter" => "arrow right" => "  "(two more spaces) => set tab width to 4(this can be done before or after.

On windows or other platforms change cmd+f and alt+enter with whatever your find and select all hotkeys are.

Note: this method is prone to "errors" if you have more than one space within your code. It is thus less safe than Magne's method, but it is faster (for me at least).

Automatico
  • 12,420
  • 9
  • 82
  • 110
  • I couldn't get this to work as such, but I used the principle and just did a find and replace (cmd-option-F on Mac) and replaced " " (4 spaces) with " " (2 spaces). And it worked! – evanbikes Sep 25 '13 at 23:45
  • I have found another way to do this now, but it is language specific. If you have a formatting-plugin like the `RubyFormat` then you can simply set the desired tab size and then do a reformat of the code. In the case of `RubyFormat` it would be `cmd+shift+R`. – Automatico Nov 25 '13 at 13:29
7

While many of the suggestions work when converting 2 -> 4 space. I ran into some issues when converting 4 -> 2.

Here's what I ended up using:

Sublime Text 3/Packages/User/to-2.sublime-macro

[
  { "args": null, "command": "select_all" },
  { "args": { "set_translate_tabs": true }, "command": "unexpand_tabs" },
  { "args": { "setting": "tab_size", "value": 1 }, "command": "set_setting" },
  { "args": { "set_translate_tabs": true }, "command": "expand_tabs" },
  { "args": { "setting": "tab_size", "value": 2 }, "command": "set_setting" }
]
Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
7

I wrote a plugin for it. You can find it here or look for "ReIndent" in package control. It mostly does the same thing as Kyle Finley wrote but in a convenient way with shortcuts for converting between 2 and 4 and vice-versa.

kamilkp
  • 9,690
  • 6
  • 37
  • 56
5

If you find search and replace faster to use, you could use a regex replace like this:

Find (regex): (^|\G) {2} (Instead of " {2}" <space>{2} you can just write two spaces. Used it here for clarity.)

Replace with 4 spaces, or whatever you want, like \t.

Qtax
  • 33,241
  • 9
  • 83
  • 121
2

You have to add this code to your custom key bindings:

{ "keys": ["ctrl+f12"], "command": "set_setting", "args": {"setting": "tab_size", "value": 4} }

by pressing ctrl+f12, it will reindent your file to a tab size of 4. if you want a different tab size, you just change the "value" number. Te format is a simple json.

  • I like it thanks! I added 2 and 4 like this: `{ "keys": ["ctrl+f10"], "command": "set_setting", "args": {"setting": "translate_tabs_to_spaces", "value": 2} }, { "keys": ["ctrl+f11"], "command": "set_setting", "args": {"setting": "tab_size", "value": 2} }, { "keys": ["ctrl+f12"], "command": "set_setting", "args": {"setting": "tab_size", "value": 4} },` – JREAM Jun 08 '17 at 10:46
1

I also followed Josh Frankel's advice and created a Sublime Macro + added key binding. The difference is that this script ensures that spacing is first set to tabs and set to a tab size of 2. The macro won't work if that's not the starting point.

Here's a gist of the macro: https://gist.github.com/drivelous/aa8dc907de34efa3e462c65a96e05f09

In Mac, to use the macro + key binding:

  1. Create a file called spaces2to4.sublime-macro and copy/paste the code from the gist. For me this is located at:

/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/spaces2to4.sublime-macro

  1. Select Sublime Text > Preferences > Key Bindings
  2. Add this command to the User specified sublime-keymap (it's in an array -- it may be empty):
{
    "keys": ["super+shift+o"],
    "command": "run_macro_file",
    "args": {
        "file":"Packages/User/spaces2to4.sublime-macro"
    }
}

Now ⌘ + shift + o now automatically converts each file from 2 space indentation to 4 (but will keep indenting if you run it further)

Sean D.
  • 1,062
  • 9
  • 12
0

The easiest thing i did was,

changed my Indentation to Tabs

and it resolved my problem.

You can do the same,

to Spaces

as well as per your need.

Mentioned the snapshot of the same.

enter image description here

Indrajeet Gour
  • 4,020
  • 5
  • 43
  • 70
  • Yes, but the question ask for the situation when you both start and end with indentation as spaces. – Magne Jan 08 '19 at 14:09
0

Recently I faced a similar problem. I was using the sublime editor. it's not an issue with the code but with the editor.

Below change in the preference settings worked for me.

Sublime Text menu -> Preferences -> Settings: Syntax-Specific:

{
    "tab_size": 4,
    "translate_tabs_to_spaces": true
}
veben
  • 19,637
  • 14
  • 60
  • 80
Ajay
  • 1
  • 1
0

with IDLE -> Format, tabify or CTRL+F5

I could not make it as simple in st

Phil
  • 708
  • 1
  • 11
  • 22