Community
    • Login

    How can i do auto replace text with python scripte... en,same as Ctrl+H

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    replacespythonscriptregex
    7 Posts 3 Posters 6.1k 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.
    • xiaokLinuxX
      xiaokLinux
      last edited by

      I want to do something like auto replace a doc (h
      ave too many regolar express),
      So I Want use python script to do this.
      But I’m freshman.(know some python)
      We are grateful to the full if you so kind as to help me.

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @xiaokLinux
        last edited by

        @xiaokLinux

        at the end it depends what exactly you want to do but a call like

        editor.rereplace(r'\bhallo\b', 'hello')
        

        would replace all words hallo of the current document with the word hello.

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 0
        • xiaokLinuxX
          xiaokLinux
          last edited by

          Thanks,It‘s works.
          But no mark to show where has changed.
          Like you use Ctrl+H and click replace all button.you can know where has changed

          Claudia FrankC 1 Reply Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank @xiaokLinux
            last edited by

            @xiaokLinux

            Like you use Ctrl+H and click replace all button.you can know where has changed

            I don’t understand what you are talking about - can you make a screenshot?
            Use syntax ![](URL_OF_UPLOADED_SCREENSHOT) to include in a post.

            Cheers
            Claudia

            Scott SumnerS 1 Reply Last reply Reply Quote 0
            • Scott SumnerS
              Scott Sumner @Claudia Frank
              last edited by

              @Claudia-Frank , @xiaokLinux :

              I think maybe that the OP is using a “change marker” plugin that puts a little symbol in the margin whenever a line in an editor buffer is changed? If so, then it is true that Pythonscript’s editor.rereplace() will not cause a line to be so marked.

              However, depending upon how great the need is, with a little more work, one can do it within the script. Below is some example code that will illustrate the point. In theory, one could deduce how their change-marker plugin marks changed lines, and then alter the logic of the script here to mark lines using exactly the same mechanism so that there aren’t TWO different kinds of margin markers littering the editor windows.

              Here’s a screenshot of what the sample script does to my Notepad++ window. The red margin marker is what comes from this code, the green is from my change-marker plugin (I use the “Location Navigate” plugin for lack of something better). That plugin uses green for saved lines, so it is pretty obvious from the screenshot that it does not know that editor.rereplace() has changed these lines.

              Here’s ReReplaceWithChangeNotations.py:

              changed_line_number_dict = dict()
              changed_line_marker_id = 0
              replace_data = None
              
              def match_fn(m):
                  (span_start, span_end) = m.span()
                  for line in xrange(editor.lineFromPosition(span_start), editor.lineFromPosition(span_end) + 1): changed_line_number_dict[line] = 1
                  return replace_data
              
              def replace_it(search, repl):
                  global replace_data
                  replace_data = repl
                  editor.rereplace(search, match_fn)
              
              def tag_changed_lines_setup():
                  margin_number_to_use = 3
                  marker_symbol = MARKERSYMBOL.FULLRECT
                  editor.markerDefine(changed_line_marker_id, marker_symbol)
                  RED_RGB_TUPLE = (255, 0, 0)
                  editor.markerSetBack(changed_line_marker_id, RED_RGB_TUPLE)
                  editor.setMarginWidthN(margin_number_to_use, 7)
                  editor.setMarginMaskN(margin_number_to_use, editor.getMarginMaskN(margin_number_to_use) | (1 << changed_line_marker_id))
              
              tag_changed_lines_setup()
              replace_it('hall'+'o', 'hello')
              for line_number in changed_line_number_dict: editor.markerAdd(line_number, changed_line_marker_id)
              
              '''
              hallo
              hallo
              hallo
              hallo
              hallo
              '''
              

              If this (or ANY other posting on the Notepad++ Community site) is useful, don’t reply with a “thanks”, simply up-vote ( click the ^ in the ^ 0 v area on the right ).

              1 Reply Last reply Reply Quote 1
              • xiaokLinuxX
                xiaokLinux
                last edited by

                cool

                It’s exactly what I wanted.
                I am truly appreciate your helping me to solved this problem.

                Scott SumnerS 1 Reply Last reply Reply Quote 0
                • Scott SumnerS
                  Scott Sumner @xiaokLinux
                  last edited by

                  @xiaokLinux

                  It occurred to me that line-change markings could also be generated as a side-effect (meaning you wouldn’t have to do ANYTHING to get them) of the technique for “find dialog automation” discussed in these threads:

                  https://notepad-plus-plus.org/community/topic/14181/execute-n-find-in-files-via-cmdline-script
                  https://notepad-plus-plus.org/community/topic/14130/hide-or-toggle-find-results-panel-with-keyboard-command

                  It is some amount of work, but it is still all Pythonscript, which you indicated you were somewhat familiar with in your original posting.

                  Basically you would use code to populate the fields in Notepad++'s Find/Replace interface, then press buttons (again via code) to do searches/replacements. Because Notepad++ itself would then take over and do the actual replacements, the line-change markings (from the plugin) would occur.

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors