Skip to content

Instantly share code, notes, and snippets.

@colinta
Created March 6, 2012 21:39
Show Gist options
  • Save colinta/1989107 to your computer and use it in GitHub Desktop.
Save colinta/1989107 to your computer and use it in GitHub Desktop.
move_text

Since there is already a binding for "m+":

Vintage/Default.sublime-keymap:

{ "keys": ["m", "<character>"], "command": "vi_set_bookmark",
    "context": [{"key": "setting.command_mode"}]
},

You might have trouble getting the keymap you want. But try this:

{ "keys": [":", "m"], "command": "move_text_to_line",
    "context": [{"key": "setting.command_mode"}]
},

Typing ":m" should activate this binding in command mode.

Now, we need a MoveTextToLine command. In your case, it sounds like you'd prefer to have this command work on the current line, not the selection, which is how MoveText works.

This is an easy command, so I'll just write it.

  1. get the region for the current line.

    cursor = self.view.sel()[0].b # "b" point of the first selection == the cursor line = self.view.full_line(cursor) # full_line includes the newline

  2. get the destination

    self.view.window().show_input_panel('Move to:', '', move_line, None, None)

  3. move the line there

    def move_line(dest_row): dest_row -= 1 # text_point is zero-indexed, not 1 dest_point = self.view.text_point(dest_row, 0) # 0th col of dest_row

       content = self.view.substr(line)
       if content[-1] != "\n":
           content += "\n"  # make sure there is a new line
    
       e = self.view.begin_edit('move_text')
       self.view.replace(e, line, '')
       self.insert(e, dest_point, content)
       self.view.end_edit(e)
    
  4. Okay, all together, see the move_text_to_line.py file. I've added support for selections (if the line is selected, instead of just an empty cursor), and some error handling.

Enjoy!

from sublime import Region
from sublime_plugin import TextCommand
class MoveTextToLineCommand(TextCommand):
def run(self, edit):
cursor = self.view.sel()[0]
if cursor.empty():
line = self.view.full_line(cursor.a) # full_line includes the newline
else:
line = cursor # support selections
def move_line(dest_row):
content = self.view.substr(line)
if not content:
return
if content[-1] != "\n":
content += "\n" # make sure there is a new line
e = self.view.begin_edit('move_text')
self.view.replace(e, line, '')
dest_row = int(dest_row)
dest_row -= 1 # text_point is zero-indexed, not 1
dest_point = self.view.text_point(dest_row, 0) # 0th col of dest_row
if dest_point >= line.a:
dest_row -= 1 # move one more up
dest_point = self.view.text_point(dest_row, 0) # 0th col of dest_row
self.view.insert(e, dest_point, content)
self.view.sel().clear()
self.view.sel().add(Region(dest_point, dest_point + len(content)))
self.view.end_edit(e)
self.view.sel().clear()
self.view.sel().add(line)
self.view.window().show_input_panel('Move to:', '', move_line, None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment