I have been using a custom shortcut to indent in Sublime Text referred in this post Indent multiple lines in Sublime Text
But it doesn't work if there are an odd number of spaces. Like my tab size is set to 2 spaces, and when there are 3 spaces at the beginning of any line it doesn't indent the remaining code.
for example:
<?php
function test_indent() {
if (condition) {
echo "here";
}
else {
echo "else";
}
}
And when I indent with the custom shortcut specified in the above post, which is:
{ "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} }
for me, it results like:
function test_indent() {
if (condition) {
echo "here";
}
else {
echo "else";
}
}
What do I need to do to make it indent properly?
While playing around with this, I discovered that if you completely unindent
the selection first, then reindent
will work as expected. It seems like it is a bug in the reindent
command that occurs when the indentation isn't a multiple of the "tab size" that is set.
Therefore, we can workaround this bug by changing your shortcut to completely unindent the selection and then re-indent it again.
From the Tools
menu, select Developer
-> New Plugin...
Select everything and replace it with the following:
import sublime
import sublime_plugin
import re
class UnindentAndReindentCommand(sublime_plugin.TextCommand):
def run(self, edit):
while any([sel for sel in self.view.sel() if re.search('^[ \t]+', self.view.substr(sel.cover(self.view.line(sel.begin()))), re.MULTILINE)]):
self.view.run_command('unindent')
self.view.run_command('reindent', { 'single_line': False })
Save it, in the location that ST suggests, as something like fix_reindent.py
- file extension is important but the base name isn't.
Then, change your keybinding to use the new unindent_and_reindent
command we just created:
{ "keys": ["ctrl+shift+f"], "command": "unindent_and_reindent" }
And it will now correctly reindent your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With