Sublime Forum

Quick Copy (without using clipboard) exists?

#1

I’m switching over to Sublime Text 2 from my long-time IDE jEdit because it doesn’t support TFS. And I’m on Windows, BTW.

[Answered via ‘Quick Paste’ feature of my script below] A feature in jEdit I miss is Quick Copy. It is like a temporary clipboard (separate from the Windows clipboard) where any text you select gets put in it automatically (no need to Ctrl-C). Then, Alt-clicking anywhere else will insert (i.e. paste) that text.

[Answered via ‘Quick Copy’ feature in my script] Also, a related technique is to put the cursor where you want some text, then go find it (usually by scrolling around) and drag over it with mouse while holding the Alt key and it will be duplicated and put back where you left your cursor… then you’ll be whisked back to your prior location. Love this feature; use it so often when I go off to find a structure name, file path, or even big old chunk of code that I want to use.

[Answered] And again, a different nuance to this is an area/block duplication… you put your cursor at the start of a block of text and then Alt-Shft-Click at the end of the block and everything in between is duplicated. So much faster than Highlight, Ctrl-C, Ctrl-V, Ctrl-V. [Found the solution for this, just select your block and use “duplicate line”. I have mine mapped to Ctrl-Shft-D, which I think is the Windows default.]

These are just small issues, but it really saves keystrokes on something that we coders do hundreds (maybe even thousands) of times a day. It also helps to keep the user’s clipboard caching program (e.g. Ditto) clean from fragments.

Does Sublime Text 2 have anything like any of these features?

Thanks for any help

1 Like

#2

None of these features exist in Sublime Text, and I’m not aware of any addons that provide them. The alt-click thing could be added with a plugin. I don’t remember if you can handle mouse drags in plugins but you might be able to.

1 Like

#3

So if I have a chunk of text that I want to copy to a part of the page that is not visible, what do ST users do to copy it… other than using the Copy/Paste/Clipboard?

(I notice that if you drag to the bottom of the window, it doesn’t scroll)

1 Like

#4

It scrolls for me… I’m not sure why it doesn’t scroll for you. Are you dragging past the edge of the window?

And yeah, I use copy and paste… isn’t that exactly what those commands are for? Copying text to another part of the page?

I’ve been thinking about this and I think it would be relatively straightforward to write plugins for both of your unanswered questions.

1 Like

#5

There is a Paste from History command that I find quite useful in this cases (ctrl+k, ctrl+v by default)

1 Like

#6

You know, I wonder if the **Transpose **command could be copied and altered slightly to get this QuickCopy effect.

Instead of flipping two selections, it could just go one way, and overwrite one of the selections with the other. It would need to overwrite the first selection with the second, leaving the second as it is. (this allows the pretty intuitive behavior of selecting what you want to replace [like a variable name] and going elsewhere in your document to find what you want to put there.)

Of course, this would depend on the program knowing which was selected first. And I don’t know if the regions are saved that way.

1 Like

#7

Regions are saved in document order. But there’s no reason you couldn’t keep track of regions and figure out which one was first.

1 Like

#8

Well, I managed to cobble together a plugin that does a decent job of what I have been talking about. I’ve been programming for a long time, but just not in Python, so I **know **this can be improved. I put a couple of TODO items in the script header in case someone else wants to help out.

The python script (currently running in ST3), which I’ve named “dgQuickCopy.py”…

[code]# Author: sugardaddy (sublimetext.com forums)

Created: May 2015

TODO: after Quick Copy is run, drop the multiple selections and put the cursor at

the end of the text that was just inserted/copied to the target location.

I’ve made attempts at it, but nothing has worked so far, so I commented it out.

TODO: Would love to have the command run when user Alt’s and then click-drags over text,

or just alt-clicks (this one good for #1)

Two main functions/features/behaviors upon calling “dg_quick_copy” command:

1) Quick Paste - Inserts last selected text … if that selected text was just one region

and current selection is just a cursor position (nothing selected).

This eliminates need to use clipboard for junky selections of text

that never need to be used again (very relevant for those who use

a clipboard caching utiltiy {like Ditto or CLCL}). Plus, because

selection storage is automatic, you’ve just eliminated the use of Ctrl-C,

which can add up by the end of the day.

2) Quick Copy - First select some text you want to replace with some other text in the editor

(or just put your cursor where you want the text copied, if no word exists

already). Then go and find the other text and add it to your selection

(usually by Ctrl-drag or Ctrl-Double-click). Then launch command (I’ve mapped mine to Alt-x), and the

region selected second will be inserted into/over the first region. You’ll

then be taken back to the first region you selected so you can resume editing.

import sublime, sublime_plugin

User configurable settings…

quickPasteMaxSizeInBytes = 100000

objects shared between the two classes

targetRegion = {}
targetText = “”

class DgQuickCopyListener(sublime_plugin.EventListener):

# Watches as text selection changes are made...
def on_selection_modified(self, view):
	global targetRegion
	global targetText
	# If we only have one selection, then it is elligible to become a target (text to 
	#  copy over) later...
	if len(view.sel()) == 1:
		for region in view.sel():
			# print('target:' + str(region.begin()) + ', ' + str(region.end()) + ' text:' + view.substr(region))
			# Will need references to this later, in the run()...
			targetRegion = region
			# Save the text selected in case we want to quick paste it later
			#  Also put in a max length so the var doesn't get too big and slow things down.
			if region.size() > 0 and region.size() < quickPasteMaxSizeInBytes:
				targetText = view.substr(region) 
			return

class DgQuickCopyCommand(sublime_plugin.TextCommand):

def run(self, edit):
	global targetRegion
	global targetText
	view = self.view
	sourceString = "" # we'll store it before erasing the target, since the coords will shift upon erasure
	# If it's two selections, then we're copying the second selection over the first one...
	if len(view.sel()) == 2:
		for region in view.sel():
			# If the region in question is not the target one, then it must be the source...
			if not (region.begin() == targetRegion.begin() and region.end() == targetRegion.end()):
				# print('source:' + str(region.begin()) + ', ' + str(region.end()))
				sourceString = view.substr(region)
				view.erase(edit, targetRegion)
				view.insert(edit, targetRegion.begin(), sourceString)
				# view.sel().clear()
				view.show(targetRegion) # scroll back to target region and continue editing, etc.
				return  # this is getting called twice, so I'm putting this return in
							# because I don't exactly know why, or how to recode it so it doesn't.
	# If just one selection, then we're doing a quick paste of the last selected string...
	if len(view.sel()) == 1:
		for region in view.sel():
			# Quick paste would also only happen if the current selection is just a cursor placement,
			#  with no text actually selected...
			if region.size() == 0 and len(targetText) > 0:
				view.insert(edit, region.begin(), targetText)
				# Below isn't working yet...
				# Want to move the cursor to the end of the inserted text, so coding/typing can resume...
				# view.sel().clear()  # this command IS working, just not next one...
				# self.view.sel().add(sublime.Region(region.begin() + len(targetText), region.begin() + len(targetText)))[/code]

ANd here’s the key mapping I put in this file (“User\Default (Windows).sublime-keymap”) to launch it…

{ "keys": "alt+x"] , "command": "dg_quick_copy" }

Hopefully those jEdit users out there that are migrating to Sublime Text will find this helpful!

1 Like

#9

You could create a Default.sublime-mousemap to map commands to (Alt-)Clicks. Check out bitbucket.org/Clams/pasteselonc … at=default

1 Like

#10

This doesn’t help the OP too much (solved anyway methinks) but for Linux users, built-in OS feature that does exactly what the OP asks, except it’s not just Sublime, it’s across all apps/windows. Just select some text somewhere, then click the centre button (or mousewheel) to paste what’s been selected. No clipboard involved.

I tend to do all development work in Linux (far nicer for dev than Windows imo) and use this feature a lot when my hand happens to be on the mouse :smile:

1 Like

#11

Thanks @duy, but the code in that example didn’t work for me. I modified it to this and can’t get it going.

{ "button": "button2" , "modifiers": "alt"] , "command": "dg_quick_copy" }

As you can see. I tried…

  • using “command” as well as “press_command”

  • using button1, button3 in addition to button2

1 Like

#12

Yeah, that’s exactly what I’m talking about! Too bad Windows doesn’t have that yet.

0 Likes