• Login
Community
  • Login

Python script to replace on selection

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 5.0k 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.
  • A
    AlexCeed
    last edited by Apr 22, 2017, 11:31 PM

    Hello,

    I’m using Python Sscript plugin v1.0.8.0 and I created a script to replace charaters and words.
    The full script is below

    with open(‘D:/config/Notepadpp/replace_char_list.txt’) as f:
    for l in f:
    s = l.split()
    if s[0] == " ":
    editor.replace(s[0], " ")
    else:
    editor.replace(s[0], s[1])

    And here is the contents of the input file

    ‘ ’
    ’ ’
    ‘ ’
    ’ ’
    “ "
    ” "
    “ "
    ” "
     

    What I’d like to do is to replace only on selection. I’ve read about editor.replaceSel but I don’t know how to use and I haven’t found any examples on the web.

    PS: the if statement is there because I couldn’t make the script replace nbsp with a space.

    Thank you

    C 1 Reply Last reply Apr 23, 2017, 4:30 PM Reply Quote 1
    • C
      Claudia Frank @AlexCeed
      last edited by Apr 23, 2017, 4:30 PM

      @AlexCeed

      Open the console and type

      >>> help(editor.replaceSel)
      Help on method replaceSel:
      
      replaceSel(...) method of Npp.Editor instance
          replaceSel( (Editor)arg1, (object)text) -> None :
              Replace the selected text with the argument text.
      

      The first argument, editor, can always be ignored because you call it from
      an editor instance, so you need just to provide the text which should be used to replace the content.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 1
      • A
        AlexCeed
        last edited by Apr 23, 2017, 8:22 PM

        @Claudia-Frank
        But I need to do a search and replace within the selection. If the first argument is the editor, and second is the text to replace, where do I add the item to search?

        C S 2 Replies Last reply Apr 23, 2017, 8:28 PM Reply Quote 1
        • C
          Claudia Frank @AlexCeed
          last edited by Claudia Frank Apr 23, 2017, 8:30 PM Apr 23, 2017, 8:28 PM

          @AlexCeed

          as the help states it replaces the selected text but from your responds
          I understand that you want to, let’s say’ select 4 lines and within this 4 lines
          you want to do a search and replace, correct?

          If this is the case you need to use the getSelection functions,
          most probably getSelectionStart() and getSelectionEnd() and use your
          replace with the returned start and end position.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • S
            Scott Sumner @AlexCeed
            last edited by Apr 24, 2017, 5:55 PM

            @AlexCeed

            To elaborate a bit on @Claudia-Frank 's explanation, here’s some code that shows how to do a replace on only the text in one or more selections:

            find = 'a'
            replace = 'A'
            num_selections = editor.getSelections()
            for sel_nbr in range(num_selections):
                start_pos = editor.getSelectionNStart(sel_nbr)
                end_pos = editor.getSelectionNEnd(sel_nbr)
                editor.replace(find, replace, 0, start_pos, end_pos)
            
            1 Reply Last reply Reply Quote 2
            4 out of 5
            • First post
              4/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors