Automatically select pasted text in Sublime Text 3

天涯浪子 提交于 2019-12-06 13:50:02

问题


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

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:

  • Select text
  • Copy
  • Press some self defined hotkey to run a macro(?)
  • This macro the pastes the text, selects the pasted text and runs the reindent hotkey (*)

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

  • ctrl+v
  • Somehow select pasted text
  • ctrl+shift+f

回答1:


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

  • from the Tools menu -> Developer -> New Plugin...
  • select all and replace with the following
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')
  • 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" },

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

How it works:

  • 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.




回答2:


On MacOS you can add:

"find_selected_text": true

to Sublime Text->Preferences->Settings (User Settings View)



来源:https://stackoverflow.com/questions/44278012/automatically-select-pasted-text-in-sublime-text-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!