Reputation: 841
Is there a way to add a keybind to reindent a highlighted block of code in Sublime Text 3?
I know there is a "reindent" option in Edit > Line > Reindent
, but it has no keybind.
Also, that reindent funcion is not so "smart" and in some cases it gives weird results. Is there an addon that better solves this issue?
I mostly code in JavaScript if that helps.
Upvotes: 0
Views: 151
Reputation: 22791
Covering the first part of your question, it's possible to bind a key to anything that exists in the Menu or command palette, it's just a matter of finding out what command and arguments you need.
The easiest way to do that would be to open the Sublime console with Ctrl+` or View > Show Console
, then enter the command sublime.log_commands(True)
, execute the command and see what it says:
>>> sublime.log_commands(True)
command: reindent {"single_line": true}
You can run the command command with False
instead of True
to turn off logging, or just restart Sublime.
With that knowledge, you can create a key binding using the command and arguments that were displayed by using Preferences > Key Bindings
and adding the binding in the right hand pane.
In this case, that woulld look something like this (change the key as appropriate):
{
"keys": ["ctrl+alt+r"],
"command": "reindent",
"args": {
"single_line": true
}
},
Once you do this, not only is the key binding active but Sublime will also display the key that you selected in the menu next to the menu item as an extra reminder for you.
For the second part of your question, indeed the internal reindent and reformat of code is not ideal in Sublime; partially this is the result of its indentation system being powered by some simple regular expressions in the same manner as TextMate for compatibility reasons.
In any case, you can search Package Control for third party packages that might allow for better formatting/reformatting of code. In the case of JavaScript, something like JsFormat might be what you want.
Typically such a package only provides integration with an external tool that does the work and thus requires you to also install an external third party tool to function. In the specific case of JsFormat however, it bundles it's own formatter directly.
Upvotes: 1