Home Download Buy Blog Forum Support

Multiple insert macro

Multiple insert macro

Postby hmalaud on Mon May 27, 2013 4:46 pm

Hello all,

I am trying to make a simple (or at least I thought) macro which is: when pressing the defined key, I add a "-" before the selected lines.
I tried all day to complete this with what was available (documentation, forums) but could not find any solution. I tried the regreplace plugin which did not work at all.

What would be a great solution too would be to automatically add a "-" in front of each new line of this specific language, but I have absolutely no idea how to do it.

If anyone has an idea, please let me know!

(sorry if this isn't the right place for this topic, I couldn't find where to make it)
Posts: 1
Joined: Mon May 27, 2013 4:40 pm

Re: Multiple insert macro

Postby miped on Mon Jul 08, 2013 8:47 pm

This is exactly the right place.

In order to insert a - in front of each line, you would have to make a TextCommand to do something like this (just the general idea):

  1. Find all the selected regions (use view.sel() to get all selections)
  2. For each region expand it to it's containing lines (use view.lines(s) for each s in view.sel())
  3. For each line, insert the character you want with view.insert() at the beginning of each line (line.begin()) using your edit object. You might have to advance the point, by the number of characters you insert so stuff stays aligned. Alternatively, you can note the line numbers using view.rowcol(line) and then convert them back into points after you prefix each line with view.text_point(row, 0)

For a more complete example from Sublime Text itself, see the file Packages/Default/comment.py in ST2, specifically the ToggleCommentCommand. In ST3 i believe the file is hiding inside the Sublime Text.app file (at least on OS X). Not sure about windows and linux.
SublimeGit: Git integration for Sublime Text 2 and 3
Posts: 16
Joined: Mon Jul 08, 2013 6:30 pm

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests