regex – How do I wrap lines in Notepad++ before N symbols?
You can set up line wrapping in Notepad++ to keep each line to a maximum of 20 characters by following a simple two-step regex Find and Replace method. This way you can manage both regular word breaks and situations where a single word is longer than 20 characters.
Table of Contents
Step 1: Wrap at the Last Space Before 20 Characters
Set Up the First Replacement:
- Find what:
(.{1,20})\s - Replace with:
$1\r\n - Explanation:
(.{1,20}): This grabs any string of 1 to 20 characters.\s: Looks for a space (or tab, etc.) right after the captured text.$1\r\n: Replaces the matched text with what was captured, followed by a new line.
Run the Replacement:
- Make sure Search Mode is set to Regular expression.
- Click Replace All.
Step 2: Handle Words Longer Than 20 Characters
Some words might go over 20 characters, making lines too long.. To fix that:
Set Up the Second Replacement:
- Find what:
(\S{20})(?=\S) - Replace with:
$1\r\n - Explanation:
(\S{20}): Captures exactly 20 non-space characters.(?=\S): Checks that the 20th character is followed by another non-space character (meaning the word isn’t finished).$1\r\n: Adds a new line after the 20-character chunk.
Run the Replacement:
- With Regular expression still selected, hit Replace All.
Example
Before Replacement:
This is a sample text thatcontainsaverylongwordthatexceedstwentycharacters and some more text.
After Step 1:
This is a sample
text
thatcontainsaverylongwordthatexceedstwentycharacters
and some more
text.
After Step 2:
This is a sample
text
thatcontainsaverylong
wordthatexceedstwenty
characters
and some more
text.
Other Info/Tips
- Backup Your Data: Back up your original text before making bulk changes
- Adjusting Limits: If you want a different character limit, just change
20with your preferred number in both regex patterns. - Plugins: For more complex text wrapping needs, check out plugins like TextFX or NppWrap for extra features and flexibility…
This two-step regex method helps you keep your text neatly wrapped without going over the character limit, dealing with both regular word breaks and those pesky long words. Hope this helps with what youre trying to do.
Read more here: Source link
