Feature to switch clipboard text with highlited text.



  • Do we have a feature where we can replace the clipboard text with the current highlighted text?

    Ex:
    Clipboard has text “XXX” and we have the text “YYY” highlighted in the text editing area. Do we have an option/feature where we can switch them so that clipboard get the “YYY” and the highlighted “YYY” get replaced by “XXX”.



  • @wessam-moustafa

    This is not a native Notepad++ function, nor is it provided by any plug-in that I’m aware of. However, a few lines of Pythonscript code can do it if you’re willing to install that plug-in…for example:

    def simple_clipboard_set_text(text_to_put_in_cb):
        doc_length = editor.getTextLength()
        curr_pos = editor.getCurrentPos()
        editor.beginUndoAction()
        editor.insertText(curr_pos, text_to_put_in_cb)
        editor.setSel(curr_pos, curr_pos + editor.getTextLength() - doc_length)
        editor.endUndoAction()
        editor.copy()  # put text in clipboard
        editor.undo()
    
    if not editor.getSelectionEmpty():
        if editor.getSelections() == 1:
            if editor.getSelectionMode() == SELECTIONMODE.STREAM:
                orig_selected_text = editor.getSelText()
                editor.paste()  # overwrite existing selection
                simple_clipboard_set_text(orig_selected_text)


  • @Scott-Sumner

    Thx a lot. That really helped :)

    Just wondering whey didn’t you use “Editor.copyText(text) → int” instead of the “simple_clipboard_set_text” function?



  • @wessam-moustafa said:

    Just wondering whey didn’t you use “Editor.copyText(text) → int”

    No good reason for not using that function in this. I think I copied the more complex code from a script I had which was doing something a bit more complicated, and totally unnecessary for swapping clipboard and selected text. Thanks for pointing out the simplification!


Log in to reply