中键将已删除的文本粘贴到Sublime 3中 [英] Middle button paste deleted text in Sublime 3

查看:64
本文介绍了中键将已删除的文本粘贴到Sublime 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:此问题不同于我正在Linux上使用Sublime Text 3(但它也适用于Sublime Text 2).

I am using Sublime Text 3 on Linux (but it also applies to Sublime Text 2).

在emacs和vim中,都可以突出显示一些文本(将其复制到剪贴板),删除该文本,然后使用鼠标中键将其粘贴到其他位置.这是我通常的工作流程,用于移动一些文本(选择->删除->中间点击).

In both emacs and vim, it is possible to highlight some text (this copies it to the clipboard), delete the text, then paste it elsewhere with the middle mouse button. This is my usual workflow to move some text (select->delete->middleclick).

但是,在Sublime上,它不会粘贴任何内容,即,中间按钮粘贴功能显然仅适用于未删除的文本.有人知道如何使它也适用于已删除的文字吗?

On Sublime however, it doesn't paste anything, i.e., the middle button paste functionality only works with non-deleted text, apparently. Does anyone know how to make it work with deleted text too?

推荐答案

我附带了这样的插件:

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 = ''

此插件将检测删除命令(向右=删除,向左=退格键)并将已删除的内容复制到内存中.然后,如果用户使用中键单击粘贴,它将用已删除的内容替换剪贴板并将其粘贴.粘贴后,它将恢复保存的剪贴板.

This plugin will detect delete comamnd (right = delete, left = backspace) and copy deleted content to memory. Then if user uses middle-click paste it replaces clipboard with deleted content and pastes it. After paste it restores saved clipboard.

我假设删除后的副本应在空白空间上工作(ST没有Vintage时没有插入模式).要更改该行为,您可以删除on_selection_modified函数以停止对它的明确检查,但是突出显示新单词不会将其复制到中间按钮剪贴板中.

I assumed that the copy from deletion should work on empty space (ST does not have insert mode without Vintage). To change that behaviour you can delete on_selection_modified function to stop checking for that explictly, however highlighting the new word will not copy it to middle-button clipboard.

使用Linux xclip的系统范围剪贴板的版本(取自pyperclip)

Version for system-wide clipboard using Linux xclip (taken from 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))

这篇关于中键将已删除的文本粘贴到Sublime 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆