Reputation: 17243
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?
Upvotes: 247
Views: 180489
Reputation: 758
with IDLE -> Format, tabify or CTRL+F5
I could not make it as simple in st
Upvotes: 0
Reputation: 17243
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.
Upvotes: 566
Reputation: 1071
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:
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
Sublime Text
> Preferences
> Key Bindings
{
"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)
Upvotes: 1
Reputation: 1
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
}
Upvotes: 0
Reputation: 4510
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.
Upvotes: 0
Reputation: 53
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.
Upvotes: 2
Reputation: 9800
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.
Upvotes: 7
Reputation: 12002
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" }
]
Upvotes: 7
Reputation: 3653
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],
Upvotes: 75
Reputation: 33928
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
.
Upvotes: 5
Reputation: 12926
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).
Upvotes: 10