I need to do several search and replaces in a macro and I also need to delete blank lines.
I've found that using TextFX Delete Blank Lines does not work in a macro (turns everything into gibberish every time).
I have searched and tried many regular expressions and can't get any to work.
The closest I can get is using extended mode to replace \n\n with \n but I have to do it several times.
For some reason, regular expression mode does not find \n or \r.
I need a way to delete blank lines using regular expressions so I can put it into a macro.
Thanks in advance
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Beware that finding all "^$" may crash Notepad++ (will loop forever in the main thread) if the last line of the file has a line end, as the search will not move the cursor passed the end of file and will thus stay in the last empty line and find it again…
Also, the regular expression replace will not let you replace the \n itself, thus will not permit to remove lines.
TextFX has several problems and should be rewritten (I removed it from my plugins because the window title incorrectly show Unicode characters with it).
Here is a script for PythonScript plugin, which will remove all "empty" lines (those matching the regular expression, which in this case includes spaces and tabs) in the whole document (replace getText by getSelText and setText by replaceSel if you want it to apply to the selection):
text=editor.getText()lines=text.splitlines(True)deltextfromcStringIOimportStringIO# See http://www.skymind.com/~ocrow/python_string/ for comparison of string methods.file_str=StringIO()importreempty=re.compile('^[ \t\n\r]*$')# Expression for matching an empty line (note that in splitLines we kept end of line characters in the string for each line)forlineinlines:ifnotempty.match(line):file_str.write(line)editor.setText(file_str.getvalue())
In my tests, this method is faster than text edit "Delete Blank Lines" (as long as there is enough memory, as this will allocate three other copies of the text, two of which might be at same address as one is deleted before the other is allocated) if you have lots of empty lines (tested on a 10k lines file of ~700kB where one third of the lines are empty).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To run the code:
- install the PythonScript plugin
- select menu Plugins->Python Script->New Script
- give it a name like "Remove empty lines.py"
- copy the script text to it and save
- now, to run it, you can select menu Plugins->Python Script->Scripts->Remove empty lines
It does not seem to be possible to run a script from a macro, but you can run a macro from a script. So if you want to remove empty lines after you run some macro, you can put in another script:
notepad.runMenuCommand('Macro', 'Name of your macro')
execfile("full path of file/Remove empty lines.py")
Use New Script to put that in another .py file, which will be accessible from the Scripts submenu.
Now to access it with some shortcut, either put it as an icon in the tool bar (see my message https://sourceforge.net/projects/notepad-plus/forums/forum/331753/topic/4397264?message=10197276) or you can use a keyboard shortcut (see the same message, but add to "Menu Items" in the configuration, restart Notepad++ and set a shortcut in Settings->Shortcut Mapper… in the "Plugin commands" tab; the script name should be in the list, somewhere, on my system it is one screen before the end of the list)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to do several search and replaces in a macro and I also need to delete blank lines.
I've found that using TextFX Delete Blank Lines does not work in a macro (turns everything into gibberish every time).
I have searched and tried many regular expressions and can't get any to work.
The closest I can get is using extended mode to replace \n\n with \n but I have to do it several times.
For some reason, regular expression mode does not find \n or \r.
I need a way to delete blank lines using regular expressions so I can put it into a macro.
Thanks in advance
A blank line is ^$
A whitespace-only line is ^\s*$
CChris
Thanks … that worked … it finds the blank lines in regular expression mode but does not delete them.
I did find: ^& replace with: <blank>
Beware that finding all "^$" may crash Notepad++ (will loop forever in the main thread) if the last line of the file has a line end, as the search will not move the cursor passed the end of file and will thus stay in the last empty line and find it again…
Also, the regular expression replace will not let you replace the \n itself, thus will not permit to remove lines.
TextFX has several problems and should be rewritten (I removed it from my plugins because the window title incorrectly show Unicode characters with it).
Here is a script for PythonScript plugin, which will remove all "empty" lines (those matching the regular expression, which in this case includes spaces and tabs) in the whole document (replace getText by getSelText and setText by replaceSel if you want it to apply to the selection):
In my tests, this method is faster than text edit "Delete Blank Lines" (as long as there is enough memory, as this will allocate three other copies of the text, two of which might be at same address as one is deleted before the other is allocated) if you have lots of empty lines (tested on a 10k lines file of ~700kB where one third of the lines are empty).
Thanks :) Do you think it will work in a macro?
… also … I don't know how to use the script you provided in notepad++ :(
To run the code:
- install the PythonScript plugin
- select menu Plugins->Python Script->New Script
- give it a name like "Remove empty lines.py"
- copy the script text to it and save
- now, to run it, you can select menu Plugins->Python Script->Scripts->Remove empty lines
It does not seem to be possible to run a script from a macro, but you can run a macro from a script. So if you want to remove empty lines after you run some macro, you can put in another script:
Use New Script to put that in another .py file, which will be accessible from the Scripts submenu.
Now to access it with some shortcut, either put it as an icon in the tool bar (see my message https://sourceforge.net/projects/notepad-plus/forums/forum/331753/topic/4397264?message=10197276) or you can use a keyboard shortcut (see the same message, but add to "Menu Items" in the configuration, restart Notepad++ and set a shortcut in Settings->Shortcut Mapper… in the "Plugin commands" tab; the script name should be in the list, somewhere, on my system it is one screen before the end of the list)
That worked perfectly! Thanks frboyer :)