(注意:此问题与Move cursor on middle button paste in Sublime Text 3)
不同我在Linux上使用Sublime Text 3(但它也适用于Sublime Text 2)。
在emacs和vim中,可以突出显示某些文本(将其复制到剪贴板),删除文本,然后使用鼠标中键将其粘贴到其他位置。这是我移动某些文本的常用工作流程(select-> delete-> middleclick)。
然而,在Sublime上,它没有粘贴任何内容,即中间按钮粘贴功能仅适用于未删除的文本。有谁知道如何使它与删除的文本一起使用?
答案 0 :(得分:2)
我带来了这样的插件:
import sublime, sublime_plugin
import re
class MyListener(sublime_plugin.EventListener):
def __init__(self):
sublime_plugin.EventListener.__init__(self)
self.deleted = ''
self.mark_for_clear = ''
self.clipboard_save = ''
self.empty_matcher = re.compile('^\s+$')
# Clear last deleted word if user made highlight of another one
# Delete if you want to e.g delete one word and highlight another to replace
def on_selection_modified(self, view):
selected_text = view.substr(view.sel()[0])
if self.mark_for_clear != '' and self.mark_for_clear != self.deleted:
self.deleted = ''
self.mark_for_clear = ''
if len(selected_text) > 0 and not self.empty_matcher.match(selected_text):
self.mark_for_clear = selected_text
def on_text_command(self, view, name, args):
# Save deleted word if command was deletion command
if name in ['right_delete', 'left_delete']:
self.deleted = view.substr(view.sel()[0])
#print("Deleted: %s " % self.deleted)
# Propagate saved deleted word to clipboard and change command to
# standard paste (we can only control standard paste clipboard)
if name == "paste_selection_clipboard" and len(self.deleted) > 0:
#print("Pasting:%s" % self.deleted)
self.clipboard_save = sublime.get_clipboard()
sublime.set_clipboard(self.deleted)
# Comment line below to enable multiple middle-click pasting of deleted words:
self.deleted = ''
return("paste", 'HackedByAlkuzad')
# If we are after paste_selection_clipboard command, return old clipboard
def on_post_text_command(self, view, name, args):
if name == 'paste' and len(self.clipboard_save) > 0 and args == 'HackedByAlkuzad':
sublime.set_clipboard(self.clipboard_save)
self.clipboard_save = ''
此插件将检测删除comamnd(right = delete,left = backspace)并将已删除的内容复制到内存。然后,如果用户使用中间点击粘贴,它将替换具有已删除内容的剪贴板并粘贴它。粘贴后,它会恢复保存的剪贴板。
我认为删除的副本应该在空白区域上工作(ST没有没有Vintage的插入模式)。要更改该行为,您可以删除on_selection_modified函数以停止检查该行为,但突出显示新单词不会将其复制到中键剪贴板。
编辑: 使用Linux xclip的系统范围剪贴板版本(取自pyperclip)
import sublime, sublime_plugin
import re
from subprocess import Popen, PIPE, check_call, CalledProcessError
class DeletedToClipboard(sublime_plugin.EventListener):
empty_matcher = re.compile('^\s*$')
def __init__(self):
sublime_plugin.EventListener.__init__(self)
try:
check_call(['which','xclip'])
except CalledProcessError:
sublime.error_message("You have to have xclip installed to use DeletedToClipboard")
@classmethod
def _is_empty(cls, text):
return len(text) <= 0 or cls.empty_matcher.match(text)
# Thanks pyperclip :)
@classmethod
def _copy_to_system_clipboard(cls, text):
# try secondary if not working
p = Popen(['xclip', '-selection', 'primary'], stdin=PIPE)
try:
p.communicate(input=bytes(text, 'utf-8'))
except Exception as e:
print("Error on paste to clipboard, is xclip installed ? \n{}".format(e))
def on_text_command(self, view, name, args):
# Save deleted word if command was deletion command and selected text was not empty
if name in ['right_delete', 'left_delete']:
deleted = []
for region in view.sel():
text = view.substr(region)
if not DeletedToClipboard._is_empty(text):
deleted.append(text)
if deleted != []:
self._copy_to_system_clipboard("\n".join(deleted))