Community
    • Login

    How do I replace a block of text with another ?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    13 Posts 4 Posters 4.9k 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.
    • Scott NielsonS
      Scott Nielson
      last edited by

      I don’t mind creating and using a template for multiple files

      1 Reply Last reply Reply Quote 0
      • Scott NielsonS
        Scott Nielson
        last edited by Scott Nielson

        If it wasn’t obvious with what is seen at that link, all my files are HTML files

        1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Scott Nielson
          last edited by

          @Scott-Nielson said in How do I replace a block of text with another ?:

          How do I replace a block of text with another ?

          It isn’t easy to do this with Notepad++, as the Replace with field doesn’t support multiline data.

          The Toolbucket plugin has a multiline search and replace capability.

          Or, a script can also do it, if you want to install the PythonScript plugin:

          # -*- coding: utf-8 -*-
          
          from Npp import editor, notepad
          
          find = editor.getSelText()
          if find == None or len(find) == 0:
              notepad.messageBox('Copy replacement text to clipboard.\r\nSelect text to find.\r\nThen re-run this script', '')
          else:
              repl = notepad.prompt('Paste text to REPLACE:', '', '')
              editor.replace(find, repl)
          

          Running the script without any text selected gives you instructions for how to use it.

          Scott NielsonS 1 Reply Last reply Reply Quote 0
          • Scott NielsonS
            Scott Nielson @Alan Kilborn
            last edited by Scott Nielson

            @Alan-Kilborn said in How do I replace a block of text with another ?:

            It isn’t easy to do this with Notepad++, as the Replace with field doesn’t support multiline data.

            Maybe a template can be created to use for multiple files but how do I go about that? The first couple and last couple of lines are the same in all files!

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Scott Nielson
              last edited by

              @Scott-Nielson said in How do I replace a block of text with another ?:

              Maybe a template can be created to use for multiple files but how do I go about that?

              I don’t know what that means – sorry.
              Maybe someone else will understand.

              The first couple and last couple of lines are the same in all files!

              I’m not sure of the relevance of that, but again, maybe someone else…

              1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Scott Nielson
                last edited by PeterJones

                @Scott-Nielson said in How do I replace a block of text with another ?:

                How do I replace a block of text with another ?

                It depends on the size of your blocks (and other conditions that you haven’t described, which I somewhat explain later).

                Let’s say you have the text

                some stuff
                
                START
                blady
                blah
                blah
                END
                
                other stuff
                

                and want it to be

                some stuff
                
                a block
                of long
                text
                
                other stuff
                

                For all examples below, SEARCH MODE = regular expression and . matches newline is assumed off

                • If they are small (like in the example I gave), then you just need to use \r\n in your search and replace to indicate the newline sequences:
                  • FIND = START\r\nblady\r\nblah\r\nblah\r\nEND
                  • REPLACE = A block\r\nof long\r\ntext
                • If the FIND block is too long to fit, then maybe there is some unique text at the start and end of the block which you can use to shorten the expression. (This is the “condition” that I mentioned earlier).
                  In my example, START and END are my creative text to make it obvious. But maybe for you, it would be <h1 class="start"> and </h1>, or something else (maybe even more complicated)
                  • FIND = (?s)START.*?END
                  • REPLACE = A block\r\nof long\r\ntext
                • If the REPLACE is too long, you obviously cannot use .* to skip entering the replacement text. However, you could break it up into chunks by choosing a temporary placeholder symbol that you know is not in your pre-existing, like ∞. You would then do multiple replacements, like follows:
                  1. FIND = (?s)START.*?END
                    REPLACE = A block∞
                  2. FIND = ∞
                    REPLACE = \r\nof long∞
                  3. FIND = ∞
                    REPLACE = \r\ntext∞
                  4. repeat as many times as are necessary to get all the lines of your replacement inserted
                  5. finally,
                    FIND = ∞
                    REPLACE = (leave empty)
                • If that’s too tedious, or if you otherwise don’t want to ignore good advice that has already been given, use @Alan-Kilborn’s example PythonScript.
                  • If you have a lot of files you want to do that to, you could even create a python array with the paths of all the files, and iterate the editor.replace(find, replace) command in a loop where you open the file, run the replacement, then save and close the file.
                • Or you could try the Toolbucket plugin which Alan already suggested: (go to Plugins > Plugins Admin, and install that plugin)
                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @PeterJones
                  last edited by

                  @PeterJones said in How do I replace a block of text with another ?:

                  REPLACE = A block\r\nof long\r\ntext

                  In general I think people are going to get ticked off about having to insert \r\n in between every line of their replacement block.
                  People just want to be able to select it or use what’s in the clipboard.
                  So… IMO currently this is quite a deficiency in Notepad++.

                  Scott NielsonS 1 Reply Last reply Reply Quote 0
                  • Scott NielsonS
                    Scott Nielson @Alan Kilborn
                    last edited by Scott Nielson

                    @Alan-Kilborn and @PeterJones I saw that Npp has something about creating a template. How do I create a template that I can add or use for multiple files?
                    I want the template to have some CSS and JavaScript (which I’ll skip adding here since I think it is unimportant to type all that) at the top apart from these meta tags:-

                    <!DOCTYPE html>
                    <html lang="en">
                    <head>
                    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
                    <meta content="IE=edge" http-equiv="X-UA-Compatible">
                    <meta content="width=device-width, initial-scale=1" name="viewport">
                    <title>Some text</title>
                    <META name="description content="Some text">
                    <META name="keywords" content="Some text">
                    <meta content="index, follow" name="robots">
                    
                    <meta content="B5jrpKjfHEj--_J-rT51c3CG8zg1sY_ZRQAbqQ1oN5Q" name="google-site-verification">
                    <link href="css/style.css" rel="stylesheet" type="text/css" media="all"><link rel="stylesheet" href="https://www.example.com/css/bootstrap.min.css">
                    <link rel="stylesheet" href="https://www.example.com/css/style1.css">
                    <link rel="stylesheet" href="https://www.example.com/css/font-awesome.min.css">
                    

                    and this at the bottom:-

                    </div>
                    </div>
                    </div>
                    </body>
                    </html>
                    

                    Please also tell me how to then add it to multiple files.
                    Thanks in advance for your time and help!

                    Alan KilbornA PeterJonesP TroshinDVT 3 Replies Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @Scott Nielson
                      last edited by

                      @Scott-Nielson said in How do I replace a block of text with another ?:

                      I saw that Npp has something about creating a template

                      I’m not sure what this would be.
                      I’d just keep a text file of the desired text, and copy/paste it into new documents as needed.

                      1 Reply Last reply Reply Quote 1
                      • PeterJonesP
                        PeterJones @Scott Nielson
                        last edited by

                        @Scott-Nielson said in How do I replace a block of text with another ?:

                        I saw that Npp has something about creating a template. How do I create a template that I can add or use for multiple files?

                        Natively, no, Notepad++ does not have templates. However, there are plugins that provide templating solutions. How to use that plugin depends on which plugin you use, and we cannot be expected to guess which you chose.

                        I want the template to have some CSS and JavaScript (which I’ll skip adding here since I think it is unimportant to type all that) at the top apart from these meta tags:

                        Then read the instructions for the template plugin you chose, and create that template.

                        Please also tell me how to then add it to multiple files.

                        In my understanding ( I don’t use templating plugins), a template is something you use when creating a new file, not something you add to multiple existing files after they’ve already been created.

                        Scott NielsonS 1 Reply Last reply Reply Quote 1
                        • Scott NielsonS
                          Scott Nielson @PeterJones
                          last edited by

                          @PeterJones @Alan-Kilborn OK, thanks guys, I’ll do the copy and paste

                          1 Reply Last reply Reply Quote 0
                          • TroshinDVT
                            TroshinDV @Scott Nielson
                            last edited by

                            @Scott-Nielson said in How do I replace a block of text with another ?:

                            @Alan-Kilborn and @PeterJones I saw that Npp has something about creating a template. How do I create a template that I can add or use for multiple files?
                            I want the template to have some CSS and JavaScript (which I’ll skip adding here since I think it is unimportant to type all that) at the top apart from these meta tags:-

                            Adds the possibility to add code snippets to the current document by selecting them from a simple list.
                            Author: Frank Fesevur
                            Homepage: https://www.fesevur.com/nppsnippets

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