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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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...
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
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
Use regular expressions
Search for (.)$
Replace with \1\\\ Search mode: Regular expressions
In selection checked
Replace all
CChris
Is there a way to use regex to place characters both before and after selected text?
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.
Ok, thanks again!
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?
Oh sorry, you did not suggest to use a macro at all, just a search and replace.
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.
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.
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.
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?
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}
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...
}
Great! I've added this to the macro. You've been a real help!
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.