Sublime Forum

Copy On Select

#1

I’d like to mimic iTerm’s “copy on select” functionality, that is, when I’m finished selecting something, I’d like it to be automatically copied to the clipboard. I thought of the on_selection_modified event and issuing view.run_command(‘copy’), but that is not going to work. It acts more like a mouse event than a selection event. It fires once on mouse_down and again on mouse_up, even if nothing is selected. I did a simple:

import sublime, sublime_plugin

class CopyOnSelectCommand(sublime_plugin.EventListener):
	def on_selection_modified(self, view):
		print view.sel()

and selecting the first line from beginning to end prints

(0, 0)]
(0, 1)]
(0, 1)]
(0, 4)]
(0, 4)]
(0, 5)]
(0, 6)]
(0, 6)]
(0, 6)]
(0, 7)]
(0, 8)]
(0, 8)]
(0, 9)]
(0, 10)]
(0, 10)]
(0, 11)]
(0, 12)]
(0, 13)]
(0, 13)]
(0, 13)]
(0, 15)]
(0, 15)]
(0, 15)]
(0, 16)]
(0, 17)]
(0, 17)]
(0, 18)]
(0, 19)]
(0, 19)]
(0, 20)]
(0, 21)]
(0, 21)]
(0, 22)]
(0, 23)]
(0, 23)]
(0, 23)]
(0, 23)]
(0, 24)]
(0, 24)]
(0, 24)]
(0, 24)]
(0, 25)]
(0, 25)]
(0, 26)]
(0, 27)]
(0, 28)]
(0, 28)]
(0, 28)]
(0, 28)]
(0, 29)]
(0, 29)]
(0, 29)]
(0, 29)]
(0, 30)]
(0, 30)]
(0, 30)]

Looking through the API documentation, I don’t see anything that I could use to make this idea work, but I wanted to ask here in case I’m missing anything.

Ideally, I’d like to have an on_selection_complete event that fires when the entire selection event is completed. Then I could just check the view to see if anything is selected and run_command(‘copy’) when necessary. Better than that, though, would be for ST2 to have a copy_on_select option (default to false, of course) that would let ST2 handle this internally. But I’m not sure if enough people would use it to warrant it being part of the core.

0 Likes

#2

Why not just store the selection in a variable, then when the sel is empty, add the variable to the clipboard?

0 Likes

#3

Pseudo code:

on_selection_modified:
if not sel.empty():
self.temp_copy = sel
else:
set_clipboard(self.temp_copy)

0 Likes

#4

[quote=“C0D312”]Pseudo code:

on_selection_modified:
if not sel.empty():
self.temp_copy = sel
else:
set_clipboard(self.temp_copy)[/quote]

Hey C0D312,

Thanks for the input. That almost works, except the selection will be true if something is selected, even on the last firing of the event on the mouse up. In fact, if you have something selected, it stays selected on a new mouse down, then clears on the mouse up. So I would not know when the selection stopped until a second mouse down. But for my purposes, that actually might work.

[clock ticking …]

So after a bit of experimenting, it might suffice, at least for now. When selecting text with the mouse, region.empty() is false when the mouse button goes up (ie, the selection is over), so I don’t know that the selection has actually ended. However, since I want this functionality so I can immediately paste what I selected, when I then click to put the insertion point where I want to paste, I get more on_selection_modified events, but this time region.empty() will be true, so I can put what I stored onto the clipboard, then paste.

I’m going to have to play with it a bit and see how it goes. The event gets fired with every keystroke as well (I’m assuming for keyboard highlighting and vintage mode-type stuff), and I don’t know if any of it will interfere. I might just forget about it and copy the old fashioned way.

Oh well. If nothing else, it’s been a learning experience. Would still like to see an on_selection_complete event, though.

0 Likes

#5

I’m having trouble understanding what the problem is. I ended up making the plugin just to see what you were taking about but it seems to behave has you requested. You seem to be upset that it doesn’t copy the text until you move the cursor. Unless you want to paste the code in another program, I don’t see this as an issue. Most people copy text so that they can place it somewhere else, which would imply the the cursor will be moved. However, the code I gave was just meant to be a base. If you’d like, I’ll make something to behave a bit better.

0 Likes

#6

Hm. Text is terrible for tone. I didn’t mean to give the impression that I’m upset, just realizing that this functionality isn’t a good fit for the current architecture. And also realizing that maybe this “functionality” isn’t all that necessary anyway. Really, I’m just trying to save a keypress or two. Besides, I just encountered a side effect that is enough to give me pause about this: if I have something in my buffer and accidentally select something, I’ve lost my original buffer.

Maybe my best course of action would be to see if I can disable iTerm’s copy on select. Then I wouldn’t even have to think about it. :smile:

[Note the smiley. Not upset here, no sir, not in the least.]

0 Likes

#7

Sorry I wasn’t more help :frowning:

As for the buffer being overridden, I use Alfred.app for OSX to manage my clipboard and gives me access to previous copies. I’m sure there’s similar apps for Windows and Linux. Also, I just had a great idea for a plugin :smile:

Picture this: press a keyboard shortcut and have the quick panel display a list of the last 10 or so clipboard contents. Then just press enter to insert it.

0 Likes

#8

Oh, but you were help! I hadn’t even thought of doing what you suggested, and, for the most part, it did work. But even more than that, I ultimately discovered that the copy on select use case, while well-suited to a terminal program, is not well-suited to an editor. In that regard, you have been a tremendous help!

[quote=“C0D312”]
As for the buffer being overridden, I use Alfred.app for OSX to manage my clipboard and gives me access to previous copies. I’m sure there’s similar apps for Windows and Linux. Also, I just had a great idea for a plugin :smile:[/quote]

I have used QuickSilver, Alfred (though a long time ago), and LaunchBar (currently using it), but multiple copy/paste buffers is not something I’ve ever been able to sqeeze into my work flow. I can see the benefit of it, and I have tried it a couple of times, but I guess I just don’t do enough of the right kind of copy/pasting to warrant it.

Given my statement above, I don’t know how useful I’d find it, but I’d definitely give it a try. You never know, this may be the switch that finally turns on the light for me!

0 Likes