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!