Community
    • Login

    Feature to switch clipboard text with highlited text.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 2 Posters 2.8k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wessam moustafaW Offline
      wessam moustafa
      last edited by

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

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Scott SumnerS Offline
        Scott Sumner @wessam moustafa
        last edited by

        @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)
        
        1 Reply Last reply Reply Quote 1
        • wessam moustafaW Offline
          wessam moustafa
          last edited by

          @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?

          Scott SumnerS 1 Reply Last reply Reply Quote 0
          • Scott SumnerS Offline
            Scott Sumner @wessam moustafa
            last edited by

            @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!

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors