• Login
Community
  • Login

Feature to switch clipboard text with highlited text.

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 2 Posters 2.4k Views
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.
  • W
    wessam moustafa
    last edited by Nov 20, 2016, 12:39 PM

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

    S 1 Reply Last reply Nov 20, 2016, 1:46 PM Reply Quote 0
    • S
      Scott Sumner @wessam moustafa
      last edited by Nov 20, 2016, 1:46 PM

      @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
      • W
        wessam moustafa
        last edited by Dec 4, 2016, 10:46 AM

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

        S 1 Reply Last reply Dec 4, 2016, 2:21 PM Reply Quote 0
        • S
          Scott Sumner @wessam moustafa
          last edited by Dec 4, 2016, 2:21 PM

          @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
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors