5

I know kate has a feature that automatically breaks a line with a newline if the line exceeds beyond a certain number of characters like 80.

Notepad++ has a word wrap feature under the Views table, but it doesn't insert a newline character. It simply splits long lines visually without inserting actual newlines.

Is there a plug-in or feature in Notepad++ to automatically break long lines using newlines automatically?

0

4 Answers 4

9

I don't know if there's a plugin, and I can't find any menu item to do this but it can be done with regular expression replace all...

  • Find: \s(?<=.{80})
  • Replace: $0\r\n

This will find the nearest space \s after 80 characters look behind (?<=.{80}) and replace with the entire match $0 followed by carriage return \r & new line \n for DOS/Windows.

Use just a new line \n for Linux.

3
  • Works perfectly - should be marked as correct answer..
    – richp10
    May 14, 2017 at 15:48
  • 1
    almost perfect - word wrapping should allow me to limit the maximum line length, so I need the last space before 80.
    – Shloim
    Jan 12, 2018 at 1:54
  • Also, pay attention to the "Search Mode" box in the lower left corner of Notepad++'s Find/Replace screen. The default radio button is "Normal". Change this to "Regular Expression", and it works like a charm. May 1, 2018 at 20:58
6

If you want Notepad++ to insert a CR/LF where it is currently showing a word wrap (based on the width of the window) then select the text and press CTRL+I -or- Edit | Line Operations | Split Lines.

This is nice because it lets you visually preview where the breaks will occur by resizing the window. You can use CTRL+J to Join lines as the inverse of this command.

This does not split at a specific column number unless your window is sized at that column. If you want to wrap at a specific column number try the Plugins | TextFX Edit | ReWrap feature. If you don't want the newlines inserted at column 72, then you need to copy the column number into the clipboard first. Unconventional, but it works.

If you used to use UltraEdit, this is similar to the "Convert wrap to line ends" feature.

1
  • 1
    I think this should be the accepted answer.
    – Reinderien
    Dec 3, 2018 at 16:54
2

This use lookbacks and lookaheads to...

  1. find the first character after 80
  2. find the consecutive non-space character set followed by (1)
  3. match the space when followed by (2)

It works for me to replace the last space before character 80 with a newline.

Find: \s(?=\S*((?<=.{80}).))
Replace: \n

Note that the Replace button in N++ does not work when inserting a \n. I think this is a bug, but using Replace All works fine. Note also that replace all will search the next 80 character block after the match (space), which should be the correct behavior for breaking up really long lines.

1
  • 1
    Nice approach, it works pretty fine.
    – Toto
    Mar 17, 2021 at 18:17
1

Find the last space before the 80th character, and split the line at that position:

  • Find (.{1,80})(\h+\R?|\R)
  • Replace \1\r\n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.