中间按钮在 Sublime 3 中粘贴已删除的文本
Middle button paste deleted text in Sublime 3
(注:本题与不同)
我在 Linux 上使用 Sublime Text 3(但它也适用于 Sublime Text 2)。
在 emacs 和 vim 中,可以高亮显示某些文本(这会将其复制到剪贴板),删除文本,然后使用鼠标中键将其粘贴到其他地方。这是我移动一些文本的常用工作流程 (select->delete->middleclick)。
然而,在 Sublime 上,它不会粘贴任何内容,也就是说,中间按钮粘贴功能显然只适用于未删除的文本。有谁知道如何让它也能处理已删除的文本?
我有这样的插件:
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 = ''
此插件会检测删除命令(右=删除,左=返回space)并将删除的内容复制到内存中。然后,如果用户使用中键单击粘贴,它会用已删除的内容替换剪贴板并粘贴它。粘贴后恢复保存的剪贴板。
我假设删除后的副本应该在空的情况下工作space(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))
(注:本题与
我在 Linux 上使用 Sublime Text 3(但它也适用于 Sublime Text 2)。
在 emacs 和 vim 中,可以高亮显示某些文本(这会将其复制到剪贴板),删除文本,然后使用鼠标中键将其粘贴到其他地方。这是我移动一些文本的常用工作流程 (select->delete->middleclick)。
然而,在 Sublime 上,它不会粘贴任何内容,也就是说,中间按钮粘贴功能显然只适用于未删除的文本。有谁知道如何让它也能处理已删除的文本?
我有这样的插件:
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 = ''
此插件会检测删除命令(右=删除,左=返回space)并将删除的内容复制到内存中。然后,如果用户使用中键单击粘贴,它会用已删除的内容替换剪贴板并粘贴它。粘贴后恢复保存的剪贴板。
我假设删除后的副本应该在空的情况下工作space(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))