Sublime Forum

Multiple insert macro

#1

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)

0 Likes

#2

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.

0 Likes