Search code examples
idesublimetext2copy-pastekeystroke

Simultaneous paste and copy of selection text


Background
I want to be able to select some text, hit a keystroke that pastes what is on the clipboard over that selection, but at the same time copying that selection to the clipboard. I often find myself doing this operation when switching variables, etc from place to place.

Example

First sentence here, I need to switch it with the second sentence below. (ctrl-c)
...
Second sentence here, I am going to put this where the first one is.

///////

First sentence here, I need to switch it with the second sentence below.
...
First sentence here, I need to switch it with the second sentence below. (ctrl-"vc" after selecting second sentence, first sentence pasted, second sentence copied now)

///////

Second sentence here, I am going to put this where the first one is. (ctrl-v)

First sentence here, I need to switch it with the second sentence below.

My question
Does anyone know if any IDE/software supports such a paste/copy functionality? Has anyone ever run into this?

More specifically, does any one know how to set up a keyboard shortcut to do this in sublime text 2?


Solution

  • You can do it with a plugin. I threw this together rather quickly. I don't do anything special for multiple cursors (though it should take the content of multiple cursors as well as paste to all the proper locations).

    import sublime
    import sublime_plugin
    
    class PasteAndCopyCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            view = self.view
            current_content = []
            cursors = view.sel()
            for cursor in cursors:
                current_content.append(view.substr(cursor))
    
            for cursor in cursors:
                view.replace(edit, cursor, sublime.get_clipboard())
    
            sublime.set_clipboard("\n".join(current_content))
    

    After you save the plugin, use paste_and_copy as the the command for your key binding or command palette entry.