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
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.
On MacOS you can add:
"find_selected_text": true
to Sublime Text->Preferences->Settings (User Settings View)