Hi! Actually, I have this habit of having all my codes/scripts indented using spaces, instead of tabs. But, obviously tapping spacebar 4 times instead of tapping tab button once, tab button is easier.
Let me show you an example, if you still did not follow what I’ve just said previously.
//example of space indentation ....function.test.(){ ........echo.'Hello World'; ....} //example of tab indentation —function.test.(){ ——echo.'Hello World'; —} // [.] is equal to 1 white space, [..] equal to 2 white spaces, and so on |
Now you see? If the indentation is using tabs, it will be showed as a long hyphen (—), while spaces will be represented by dot (.) . So, I don’t want that long hyphen exist in my code as it will disrupt the codes indentation if I open the codes in other Text Editor. (Some text editor really messed up my code indentation badly if I use tabs)
Having said all that, I’ll use tab every time I indent my code since, obviously, it’s easier. Sublime Text 2 have this features where you can converts all tabs to spaces, and vice versa. I really do love this feature. But, by default, this converter didn’t have a shortcut key assigned to it. Which, in my case, who really do love using shortcut key, is a bit of a hassle when I need to click View > Indentation > Convert Indentation to Spaces. Switching from keyboard to mouse/trackpad slows me down.
Ok. enough explanation. let’s get to the main point. List of things to do to make a shortcut key for this matter.
- Create our own custom package
- Bind shortcut key to run that package
Creating our own Custom Package
- Fire up the Preference finder (ctrl+shift+P) and look for Browse Package. You’ll be popped up with the Packages folder.
- Create a new folder (name it as you wish) in the Packages folder. In this example, I’ll name it as “TabSpaceConverter”.
- Create a new file named “convert_tabs_to_spaces.py”. Upon creating this new file, you’ll notice a similar filename with the .pyc extension will be auto-created. Don’t panic. it’s normal. Just leave it be.
- Open that File and paste below code:
import sublime, sublime_pluginclass ConvertTabsToSpaces(sublime_plugin.TextCommand): def run(self, edit): self.view.run_command('expand_tabs', {"set_translate_tabs": True})
- Don’t forget to save the code!
Next, binding the function to a shortcut key
- Go to menu bar. Preferences > Key Bindings – User.
- Add the command and keys as you prefer it to be. See example below:
{ "keys": ["ctrl+alt+s"], "command": "convert_tabs_to_spaces" },
- And, try it for yourself!
If you prefer tabs instead of spaces, you could follow again step 3 – 5 for the custom package, and add another shortcut key for it in the Key Bindings – User. Use below code:
import sublime, sublime_plugin class ConvertSpacesToTabs(sublime_plugin.TextCommand): def run(self, edit): self.view.run_command('unexpand_tabs', {"set_translate_tabs": False}) |
Key Bindings – User:
{ "keys": ["ctrl+alt+t"], "command": "convert_spaces_to_tabs" }, |
Tutorial Source : StackOverflow
Hope that’ll help you as well.