• Login
Community
  • Login

Mass replace inside file at once

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 1.6k 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
    Andy Light
    last edited by Feb 6, 2020, 2:39 PM

    Hi. Is any way to replace multiple values inside one file at once?
    For example I want to replace"green" with “red” and “blue” with “black”. To do this, I must:

    1. Open Replace and enter in Find what word “green” and in Replace with word “red”. Then press Replace All.
    2. Then open Replace and enter in Find what word “blue” and in Replace with word “black”. Then press Replace All.

    Maybe there is some way to replace “green” with “red” and “blue” with “black” at once?

    A 1 Reply Last reply Feb 6, 2020, 2:58 PM Reply Quote 0
    • A
      Alan Kilborn @Andy Light
      last edited by Feb 6, 2020, 2:58 PM

      Yes, use a Regular Expression Search mode replacement to search for (green)|(blue) and replace with (?1red)(?2black).

      I think you’ll be able to see the pattern in that to extend it to other colors/searches.

      A 1 Reply Last reply Feb 6, 2020, 4:26 PM Reply Quote 4
      • A
        Andy Light @Alan Kilborn
        last edited by Feb 6, 2020, 4:26 PM

        @Alan-Kilborn thank you!

        A 1 Reply Last reply Feb 7, 2020, 12:44 PM Reply Quote 1
        • A
          Alan Kilborn @Andy Light
          last edited by Feb 7, 2020, 12:44 PM

          @Andy-Light

          Maybe it is also worth pointing out, if you need to do this same replacement often, you can record multiple replacement operations into a “macro”, give the macro a descriptive name, and then just run the macro when you need to. This saves the step of remembering the syntax I provided earlier.

          In summary:

          • begin macro recording
          • run as many individual replacement operations as you need to (two in the above case, your steps #1 and #2)
          • stop the macro recording
          • save the macro, give it a name
          • when you need to run it, find the name you gave it on the Macro menu and choose it

          All of the things you need are on the Macro menu (and also in the toolbar).

          The only downside: Say you are doing 10 individual replacements. You have to get them all correct on the first try while recording the macro, with no other intervening operations (as they would also be recorded into the macro). Not too difficult, but it must be kept in mind.

          1 Reply Last reply Reply Quote 1
          • P
            PeterJones
            last edited by PeterJones Feb 7, 2020, 2:58 PM Feb 7, 2020, 2:57 PM

            Say you are doing 10 individual replacements. You have to get them all correct on the first try

            “on the first try” is not likely, for me. :-) Personally, especially if they all used the same options, I would “do my best” on getting one or two recorded, then save and reload Notepad++, open shortcuts.xml, and copy/paste and manually edit the recording to get the others, save the file and reload Notepad++ again. Or record them as 10 separate macros, save and reload, then merge them manually in shortcuts.xml, save and reload Notepad++.

            Docs:

            • Macros overview: https://npp-user-manual.org/docs/macros/
            • <macros> section of shortcuts.xml: https://npp-user-manual.org/docs/config-files/#macros
            • Search-specific macro syntax: https://npp-user-manual.org/docs/searching/#searching-actions-when-recorded-as-macros
            A 1 Reply Last reply Feb 7, 2020, 6:09 PM Reply Quote 3
            • A
              Alan Kilborn @PeterJones
              last edited by Feb 7, 2020, 6:09 PM

              @PeterJones said in Mass replace inside file at once:

              merge them manually in shortcuts.xml

              For you, yes, but for a casual Notepad++ user, maybe not.
              I was just pointing out that a bit of serious concentration when recording macros will go a long way in helping out. :-)

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