Search code examples
selecttabssublimetextindentationsublimetext3

Sublime Text 3: Keep entire lines selected when indenting with tab


I have an issue that is driving me insane with Sublime Text 3. When I select multiple lines by clicking on the side of the line and dragging, I then hit tab to correct the indentation, but then I want to move the entire line to another spot except I have to re-select it because the first line is only selected from where the text starts, not where the line starts.

Let's see if I can illustrate this... Below is the lines I have:

Text Line 1
Text Line 2

I select them (selection shown using *)

*Text Line 1
Text Line 2*

I indent the lines and now the selection looks like this:

    *Text Line1
    Text Line 2*

Notice the selection starts with the text. I want the selection to stay at the beginning of the line like this:

*   Text Line 1
    Text Line 2*

I have searched everywhere but apparently I'm the only one that is bothered by this. Every other code editor I have used does it the way I want.


Solution

  • You can achieve this will a small Python plugin:

    1. Tools -> Developer -> New Plugin...
    2. Replace the contents with the following:

      import sublime
      import sublime_plugin
      
      
      class IndentSelectWholeFirstLineEventListener(sublime_plugin.EventListener):
          def on_post_text_command(self, view, command_name, args):
              if command_name == 'indent':
                  if all(not sel.empty() for sel in view.sel()):
                      if all(view.line(sel.begin()) != view.line(sel.end()) for sel in view.sel()):
                          new_selections = []
                          for sel in view.sel():
                              new_selections.append(sel.cover(view.line(sel.begin())))
                          view.sel().clear()
                          view.sel().add_all(new_selections)
      
    3. Save it in the folder ST suggests, as fix_selection_after_indent.py

    How this works:

    Immediately after the indent command is executed, if all the selections are not empty, and they all span multiple lines, extend the selections to cover the entire first line of each selection.