Blank Trim operations, on a text selection, ONLY
-
Hello Noitidart and All,
Noitidart, I reply to your last post, on Github N++ forums, at the address :
https://github.com/notepad-plus-plus/notepad-plus-plus/issues/302#issuecomment-115071964
As I said before, the N++ Blank Trim Operations, logically, work on the totality of the current file. Luckily, it’s easy enough to use a search/replacement, in regular expression mode, to get rid of these blank characters, in a normal selection, ONLY.
And, of course, you can store this specific search-replacement in a macro, which can be, easily, executed, further, from the macro Menu OR by hitting its associated shortcut !
Let’s suppose you would like a macro to delete all trailing blanks in a selection. To do so :
-
Open any text file
-
First, select some lines ( even without any trailing blank )
-
Select the menu option Macro - Start recording OR the red circle button, on the Toolbar
-
Open the Replace dialog ( CTRL + H )
-
In the Find what zone, type
\h+$
-
Leave the Replace with zone empty
-
Check the In selection option
-
Select the Regular expression search mode
-
Click on the Replace All button
-
Select the menu option Macro - Stop Recording Or the black square button, on the Toolbar
-
Open the menu option Macro - Save Currend recorded Macro…
-
Type, for instance, Trim SELECTION trailing BLANK characters as the current name for this macro
-
Choose a shortcut for this macro ( Take care NOT to use an existing Notepad++ or plugin shortcut ! )
-
Click on the OK button, to valid your choices
Now, each time you select a normal selection ( neither rectangular selection, nor multiple selections ), any trailing blank character will be removed of that selection, as soon as you choose the menu option Macro - Trim SELECTION trailing BLANK characters OR you use the its specific shorcut
Without any previous normal selection, this macro doesn’t do anything. Nice isn’t it ?
Best regards,
guy038
-
-
Oh wow I didn’t know we could save things as a macro! Thanks very very much for this detailed instructions Im going to follow it right away!
And this new community is absolutely beautiful! Is it public software or custom?
edit: just tested it and it works so awesome! thank you!
btw what language uses
\h
i can’t find that symbol in any regex tutorials, what is it btw? -
Hi Noitidart,
I think that this N++ community is based on NodeBB forums. You’ll get further information, from the link below :
See also the Don Ho post :
https://notepad-plus-plus.org/community/topic/2/welcome-to-notepad-community
Secondly, you’ll find good documentation, about the new Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by Notepad++, since its
6.0
version, at the TWO addresses below :http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
-
The FIRST link explains the syntax, of regular expressions, in the SEARCH part
-
The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part
As for the \h regex syntax, just have a look to the paragraph “Single character” character classes, in the SEARCH document !
Best regards,
guy038
-
-
Thanks @guy038 for all that info this is real cool!
-
each time you select a normal selection ( neither rectangular selection, nor multiple selections )
Without any previous normal selection, this macro doesn’t do anythingI recently had a need to record a replace macro that operated only upon selected text. What I found was different from what @guy038 reported about multiple selections. I found that if multiple selections are active when the macro is invoked, it WILL perform the replacement, but only in the selection created most recently (this is consistent with how Notepad++ performs this replacement interactively).
-
Hi @guy038,
If
\h+$
removes trailing spaces, what regex would remove leading and trailing spaces? I’m looking for the equivalent of Edit -> Blank Operations -> Trim Leading and Trailing Space, but on selected text only. -
Try this:
Find what zone:
(?-s)^\h*(.*?)\h*$
Replace with zone:\1
In selection checkbox: ticked
Search mode:Regular expression
Action: Press Replace All button