Menu

Macro to insert characters after each line

2009-08-27
2012-11-13
  • greenappkle

    greenappkle - 2009-08-27

    I'm wondering if anyone knows how to create a macro that will enter characters (in my case, three backslashes) after every line of text that is selected.  I've figured out how to create a macro that inserts characters at the very end of a block of selected text, but not at the end of each line of a block of selected text. 

    Thank you!

     
    • cchris

      cchris - 2009-08-29

      NThere isn't, because regular expression matching is a process which does not assume any selected property. All that Notepad++ can do is to restrict the search to the selected area - hmm, soon _a_ selected area.

      CChris

       
    • cchris

      cchris - 2009-08-27

      Use regular expressions
      Search for (.)$
      Replace with \1\\\ Search mode: Regular expressions
      In selection checked
      Replace all

      CChris

       
      • greenappkle

        greenappkle - 2009-08-28

        Is there a way to use regex to place characters both before and after selected text?

         
        • François-R Boyer

          If what you wanted to say is "before and after each line", yes.  For example, if you want to have each line placed in a C comment, just ask to replace "(.*)" by "/* \1 */"  (without the quotes).  Since NP++ search is line by line, the ".*" will select a complete line (otherwise we can search "^" for line begin, and "$" for line end). The parenthesis are there so the matched text can be refered later as "\1".

          If what you want is add only once before and after a whole selected text, you should use a macro.  It seams that the simplest way is to "ctrl-x" (cut), insert what you want at beginning, "crtl-v" (paste), insert what you want at end.

           
          • greenappkle

            greenappkle - 2009-08-29

            Ok, thanks again!

             
      • Gerrit van Niekerk

        Search (and Replace) are not supported in a macro as far as I know; I even have an open thread about that. Have I missed anything?

         
      • Gerrit van Niekerk

        Oh sorry, you did not suggest to use a macro at all, just a search and replace.

         
      • greenappkle

        greenappkle - 2009-08-27

        Thanks for the suggestion CChris!  It sounds like you are recommending this approach in place of using a macro.  In any event, I'll play around this and see if it adequately addresses my needs.

         
    • greenappkle

      greenappkle - 2009-08-27

      Unfortunately, I don't think this is going to do it for me.  It's going to end up taking more time to use the search and replace feature (in addition to remembering the search and replace terms) then to enter the backslashes myself.  I appreciate the suggestion nonetheless. Hopefully, Notepad++ will support search and replace macros some time in the near future.

       
      • François-R Boyer

        I know it may not be the cleanest solution... but in the mean time of having Notepad++ correctly support replace in macros, you could use an external macro scripting tool like AutoHotkey.  Your script could "ctrl-h", type text for search and replace, "alt-g" for regular expression, "alt-a" for replace all, "enter" and "alt-f4" to close the search window.  And to prevent this macro to execute in other applications, you can check that Notepad++ is the active window.

         
        • greenappkle

          greenappkle - 2009-08-27

          Thanks for your suggestion frboyer.  I tried playing around with AutoHotkey, but haven't been able to get it to work in Notepad++ yet.  Is there a way to assigned a shortcut key to complete all of the tasks (opening search and replace, choosing regular expression, type text into Find what: and Replace with: boxes, hit replace all, etc.) or would have I have to assigned a hotkey to complete each of these steps?

           
        • greenappkle

          greenappkle - 2009-08-27

          This ended up being a superb recommendation frboyer!  I got the whole series to work.  Here is my AutoHotkey script in case anyone else is interested.

          Send ^r!{g}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}(.)${tab}\1 ///{tab}{Space}!{a}{Enter}!{F4}

           
          • François-R Boyer

            If you want to check that Notepad++ is currently active, before sending the macro (which could do weird things in other windows), you can add:
                WinGetClass, ClassName, A
                if ClassName = Notepad++
                {
                  macro code here...
                }

             
            • greenappkle

              greenappkle - 2009-08-28

              Great!  I've added this to the macro.  You've been a real help!

               
        • greenappkle

          greenappkle - 2009-08-27

          I should explain what this script does, in case anyone else is interested in trying to do something similar with AutoHotkey.

          ^/::Send ^r!{g}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}(.)${tab}\1 ///{tab}{Space}!{a}{Enter}!{F4}

          The script starts out by assigning Ctrl+/ (^/::) to run the macro, it then sends Ctrl+r to open the search and replace box, then Alt+g to select the "Regular expression" radio button, then 11 tabs, then types "(.)$" (w/o quotes)  into the "Find what:" box, then tabs, then types "\1 ///" (again w/o quotes)  into the " Replace with:" box, then tabs, then Spacebar to check the "In selection" checkbox, then Alt+a to select "Replace All", then Enter, and finally Ctrl+F4 to close the search and replace box.