自动在Sublime Text 3中选择粘贴的文本 [英] Automatically select pasted text in Sublime Text 3

查看:220
本文介绍了自动在Sublime Text 3中选择粘贴的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过插件,宏或其他方式使Sublime Text 3自动选择刚刚粘贴的文本?

Is there any way, plugin, macro or something to make Sublime Text 3 automatically select the text that was just pasted?

我需要复制并粘贴一些JSON数据,但是粘贴的文本永远不会与周围的文本对齐.粘贴和缩进-功能无法正常运行.

I need to copy and paste some JSON data, but the pasted text is never in line with the surrounding text. Paste and indent -feature does not work properly for this.

起作用的是缩进功能,但这需要我选择一个文本块并按一个热键.因此,在粘贴之后,我将受益于自动选择刚刚粘贴的文本块,因此我只需按重新插入热键即可正确缩进粘贴的内容.

What does work is the reindent feature, but it requires me to select a block of text and pressing a hotkey. So after pasting I would benefit for having the just pasted block of text being automatically selected, so I can just press the reindent hotkey to properly indent what I pasted.

此外,如果我可以将整个过程绑定到热键,那就更好了,所以:

Furthermore, it would be even better if I could bind the whole process to a hotkey, so:

  • 选择文本
  • 复制
  • 按一些自定义的热键以运行宏(?)
  • 此宏粘贴文本,选择粘贴的文本并运行重新插入热键(*)

*因此,基本上,我想进行键盘绑定,例如ctrl + shift + b来执行以下操作:

*So basically I would like to make a keybinding, say, ctrl+shift+b to do the following:

  • ctrl + v
  • 以某种方式选择粘贴的文本
  • ctrl + shift + f

推荐答案

您可以创建一个插件来执行此操作,并使用键盘绑定执行该插件:

You can create a plugin to do this, and execute it with a keybinding:

  • 从工具"菜单->开发人员->新插件...
  • 全选并替换为以下内容
import sublime
import sublime_plugin


class PasteAndReindentCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        before_selections = [sel for sel in self.view.sel()]
        self.view.run_command('paste')
        after_selections = [sel for sel in self.view.sel()]
        new_selections = list()
        delta = 0
        for before, after in zip(before_selections, after_selections):
            new = sublime.Region(before.begin() + delta, after.end())
            delta = after.end() - before.end()
            new_selections.append(new)
        self.view.sel().clear()
        self.view.sel().add_all(new_selections)
        self.view.run_command('reindent')

  • 将其保存,在文件夹ST中建议(Packages/User/)类似于paste_and_reindent.py
  • 将以下内容添加到您的用户键绑定{ "keys": ["ctrl+shift+b"], "command": "paste_and_reindent" },
    • save it, in the folder ST suggests (Packages/User/) as something like paste_and_reindent.py
    • add the following to your User keybindings { "keys": ["ctrl+shift+b"], "command": "paste_and_reindent" },
    • 请注意, Ctrl + Shift + B 将替换"Build With"的默认绑定.

      Note that Ctrl+Shift+B will replace the default binding for "Build With".

      工作原理:

      • 按下键绑定时,它将运行在插件中创建的新命令
      • 这将存储当前的文本选择位置
      • 然后执行粘贴操作
      • 然后获取新的文本插入符号位置
      • 然后将旧位置与新位置进行比较,并选择粘贴的文本
      • 然后运行reindent命令
      • when the keybinding is pressed, it runs the new command created in the plugin
      • this stores the current text selection positions
      • then it performs the paste operation
      • then it gets the new text caret positions
      • then it compares the old positions to the new ones, and selects the text that was pasted
      • then it runs the reindent command

      通过重新比较缩进前后的选择,您可以使它随后再次清除选择(通过将文本插入符号重新定位到选择的末尾-即粘贴后的默认行为).

      You could get it to clear the selections again afterwards (by repositioning the text carets to the end of the selections - i.e. the default behavior after pasting) by doing another comparison of the selections before and after the reindentation.

      这篇关于自动在Sublime Text 3中选择粘贴的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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