[sublimetext2] How to fix/convert space indentation in Sublime Text?

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?

This question is related to sublimetext2 indentation auto-indent

The answer is


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.


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.


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


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" }
]

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 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],

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
}

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).


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)


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.


Examples related to sublimetext2

Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? How to Install Sublime Text 3 using Homebrew 80-characters / right margin line in Sublime Text 3 Comparing the contents of two files in Sublime Text What is the default font of Sublime Text? Showing the same file in both columns of a Sublime Text window What is the difference between Sublime text and Github's Atom Sublime Text 2 multiple line edit Set Encoding of File to UTF8 With BOM in Sublime Text 3 Regex replace uppercase with lowercase letters

Examples related to indentation

How to indent/format a selection of code in Visual Studio Code with Ctrl + Shift + F Python IndentationError unindent does not match any outer indentation level How to change indentation mode in Atom? How to set HTML Auto Indent format on Sublime Text 3? "Expected an indented block" error? Indent starting from the second line of a paragraph with CSS Indentation Error in Python How to fix/convert space indentation in Sublime Text? How to make the tab character 4 spaces instead of 8 spaces in nano? IndentationError: unexpected unindent WHY?

Examples related to auto-indent

Brackets.io: Is there a way to auto indent / format <html> How to auto-indent code in the Atom editor? Sublime Text 3, convert spaces to tabs How to fix/convert space indentation in Sublime Text? How To Auto-Format / Indent XML/HTML in Notepad++ Difference between Ctrl+Shift+F and Ctrl+I in Eclipse Turning off auto indent when pasting text into vim Tab key == 4 spaces and auto-indent after curly braces in Vim