Reputation: 4693
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:
*So basically I would like to make a keybinding, say, ctrl+shift+b to do the following:
Upvotes: 3
Views: 1132
Reputation: 808
On MacOS you can add:
"find_selected_text": true
to Sublime Text->Preferences->Settings (User Settings View)
Upvotes: 1
Reputation: 16085
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')
Packages/User/
) as something like paste_and_reindent.py
{ "keys": ["ctrl+shift+b"], "command": "paste_and_reindent" },
Note that Ctrl+Shift+B will replace the default binding for "Build With".
How it works:
reindent
commandYou 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.
Upvotes: 5