Unwrap text in Sublime Text 2 - regex

I'd like to unwrap lines so that I can turn them from lines with hard line-breaks to no line breaks.
Specifically, this means that contiguous runs of lines with non-whitespace should be joined together Essentially, any \n with no whitespace on either side should be replaced with a single space. Other linebreaks shouldn't get touched.
I feel like it ought to be a search-and-replace with a search string something like (?!\n)\n(?!\n) -> , but that doesn't work, as it doesn't match anything.
Is there an ST2 built-in command for this?

any \n with no whitespace on either side
(?<!\s)\n(?!\s)
other linebreaks shouldn't get touched.
(?<!(?:\s|\n))\n(?!\s)
Replace with ''

As #flow mentioned, there are built-ins for that task. Just select the lines you want to join and press Ctrl + J.
And your way should works too. Only you missed a bit. It should be (?<!\n)\n(?!\n)

The following solution works best for text copied from a console log with 80 columns. It only removes \n if the line touches the last column.
Find:
(.{80})\n
Replace:
$1

Related

Remove any blank or whitespaced lines using regex

I am trying to find and remove any blank or whitespaced lines in OpenOffice document using regex.
Currently I can do it in two steps:
Search for ^$ and replace with nothing.
This will remove any empty lines.
Search for ^\s*$ and replace with nothing.
This will remove any lines which contains only spaces or tabs.
Important note: From my point of view, this 2nd version should also remove any empty lines (as 1st version), but actually it doesn't.
So, there are actually two questions.
For what reason second regex matches only lines with spaces and tabs, but don't match empty lines?
Is there way to combine first and second version to achieve desired result in one step? Here what I tried: ^$|^\s*$ and (^|^\s*)$. But it doesn't work. It matches only whitespaced lines, but not empty ones.
Text for test:
Just for example, I changed spaces to dots
and tabs to dashes.
aa
..........................
-------------------
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa
Desired result:
aa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa
AltSearch can do this as a single step using a Batch script. In the AltSearch dialog, click on Batch >>. Then Edit the file and paste the following code at the end.
[Name] Remove any blank or whitespaced lines
; Remove any lines which contains only spaces or tabs.
[Find]^\s*$
[Replace]
[Parameters] MsgOff Regular
[Command] ReplaceAll
; Remove any empty lines.
[Find]^$
[Replace]
[Parameters] MsgOff Regular
[Command] ReplaceAll
[End]
Now, save the text file and click Refresh. Finally, click on Remove any blank or whitespaced lines and press Execute.
This produces the desired result and shows a single dialog:
Batch 'Remove any blank or whitespaced lines' is ended.
10 replacements have been done.
Since the title of your question reads like asking for a pure regex (which is why I found it):
\s(?=\s)
Simply replace its matches with nothing - see Regex101 and Regexr.

How do I replace a newline in Atom?

In Atom, If I activate regex mode on the search-and-replace tool, it can find newlines as \n, but when I try to replace them, they're still there.
Is there no way to replace a newline-spanning string in Atom?
Looks like Atom matches newlines as \r\n but behaves inconsistently when replacing just the \n with nothing.
So newlines seem to match \s+ and \r\n, and only "half" of the line-ending matches \n.
If you replace \n with a string, nothing happens to the line-ending, but the string is appended to the next line
If you replace \r with a string, nothing happens at all, but the cursor advances.
It's alittle bit late to answer but i use following term to search and it works with Atom v1.19.7 x64
\r?\n|\r
BR
None of these answers helped me.
What worked for me:
I just added a new line at the end of the file.
Shift + <- (arrow to left)
Ctrl + C
Ctrl + V in the "Replace in current buffer" line
Just copied the new line and pasted it in :D
DELETE INVISIBLE LINE BREAKS IN CODE WITH ATOM
(using the "Find in buffer" function)
(- open your code-file with the Atom-Editor)
Hit cmd(mac)/ctrl(win) + f on your keyboard for activating the Find in buffer function (a little window appears at the bottom atom-screen edge).
Mark your Code in which you want to delete the invisible Line breaks.
Click on the Markup-Mode Button and after that on the Regex-Mode (.*) Button and type into the first field: \n
After that click replace all.
[And Atom will delete all the invisible line breaks indicated by \n (if you use LF-Mode right bottom corner, for CRLF-Mode (very common on windows machines as default) use \r\n) by replacing them with nothing.]
Hope that helps.
Synaikido
You can use backreferencing:
eg. Replace triple blank lines with a single blank line
Find regex: (\r\n){3}
Replace: $1
You can indicate double blank lines with (\r\n){2} ... or any number n of blank lines with (\r\n){n}. And you can omit the $1 and leave replace blank to remove the blank lines altogether.
If you wanted to replace 3 blank lines with two, your replace string can be $1$1 or $1$2 (or even $1$3 ... $3$3 ... $3$2 ... ): $1 just refers to the first round bracketed expression \r\n; $2 with the second (which is the same as the first, so $1$1 replaces the same way as $1$2 because $1 == $2). This generalizes to n blank lines.
The purists will probably not like my solution, but you can also transform the find and replace inputs into a multiline text box by copying content with several line breaks and pasting it into the find/replace inputs. It will work with or without using regex.
For example, you can copy this 3 lines and paste them into both find and replace inputs:
line 1
line 2
line 3
Now that your inputs have the number of lines that you need, you can modify them as you want (and add regex if necessary).
Heh, very weird, Ctrl+Shift+F does not work too!
Workaround: open Atom Settings, then Core Packages->line-ending-selector, scroll to bottom to see tips about command to convert line endings: 'convert-to-LF'.
To convert: Cmd+Shift+P type 'line' and choose 'convert-to-LF' - done!
You could change default option 'Default line ending' from 'OS' to 'LF'.
Also after settings changed your new files will use 'LF'.
prerequisite: activate 'Use Regexp'
in my version of atom (linux, 1.51.0) i used the following code to add 'export ' after a new line
search '\n'
replace '\nexport '
worked like a charm
\r\n didn't match anything

Notepad++ Regex for remove text in line after first two words

so I have a list that goes like this:
AudioQuest FLX-14/2
Abbey Road Cable Monitor Speaker Cable
and in in the first line I need to remove everything after first word and in the second one I need to remove everything in line after first TWO words. I figured out how to remove everything after first word, it's
.*?$
but I'm helpless with the second case. Help me out so I can toggle shortcuts on macros for both actions and process the list in the way semi-automatical way (Select and apply macros).
From what I can see, it seems the data is aligned. And from the example, only the first 10 characters are needed, the rest should be removed.
Find what: (.{10}).*
Replace with: $1
I'd do it in two passes..
Find:
"(^[a-z] [a-z]* )"
"(^[a-z] )"
Replace: "\1"

I need a regex to repair lines split at column 80

Problem - Multiline, Semi-colon delimited file has been split at column 79 or 80 (not always the same for some strange reason).
It seems to me that a Regex would be the appropriate solution, so now I have two problems.
Lines are:
1sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
.....|.....|.....|.....|.....|.....|[cr][lf]
2sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
.....|.....|.....|.....|.....|.....|[cr][lf]
3sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
.....|.....|.....|.....|.....|.....|[cr][lf]
4sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
.....|.....|.....|.....|.....|.....|[cr][lf]
... 10000 rows ...
Where the pipe is a non-space whitespace character (possibly a tab)
I need:
1sdf.............................mnopqr........xyz......................[cr][lf]
2sdf.............................mnopqr........xyz......................[cr][lf]
3sdf.............................mnopqr........xyz......................[cr][lf]
4sdf.............................mnopqr........xyz......................[cr][lf]
I managed to get the job done with
Pass 1:
Replace ^\s*\r\n with \rxxx\n
// Replace Blank lines with \rxxx\n leaving
1sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
[cr]xxx[lf]
2sdf.............................mno[cr][lf]
pqr........xyz......................[cr][lf]
Pass 2:
Replace \r\n with [empty]
//leaving:
1sdf.............................mnopqr........xyz......................[cr]
xxx[lf]
2sdf.............................mnopqr........xyz......................
Pass 3:
Replace \rxxx\n with \r\n
//leaving:
1sdf.............................mnopqr........xyz......................[cr][lf]
2sdf.............................mnopqr........xyz......................
And the rest of the cleanup is trivial.
Is there any way of doing this in a single step? The output is from a common financial application, and I'd rather be able to fix the files myself rather than try and get many multiple clients to adjust their output.
In Notepad++ (using regular expression mode) you can use this:
Find what: \r\n(\s*\r\n)?
Replace with: \1
Then run "Replace All" exactly once. However, make sure you update to Notepad++ 6! Otherwise matching \r\n with a regular expression won't work in Notepad++.
Assuming that ^\s*\r\n match the line you want to remove as you said above, I believe you could do it with replacing \r\n\s*\r\n|\r\n by \r\n
It's my first regex, so if it doesn't work, don't be to harsh :-)
Good luck

Removing empty lines in Notepad++

How can I replace empty lines in Notepad++? I tried a find and replace with the empty lines in the find, and nothing in the replace, but it did not work; it probably needs regex.
There is now a built-in way to do this as of version 6.5.2
Edit -> Line Operations -> Remove Empty Lines or Remove Empty Lines (Containing Blank characters)
You need something like a regular expression.
You have to be in Extended mode
If you want all the lines to end up on a single line use \r\n. If you want to simply remove empty lines, use \n\r as #Link originally suggested.
Replace either expression with nothing.
There is a plugin that adds a menu entitled TextFX. This menu, which houses a dizzying array of quick text editing options, gives a person the ability to make quick coding changes. In this menu, you can find selections such as Drop Quotes, Delete Blank Lines as well as Unwrap and Rewrap Text
Do the following:
TextFX > TextFX Edit > Delete Blank Lines
TextFX > TextFX Edit > Delete Surplus Blank Lines
notepad++
Ctrl-H
Select Regular Expression
Enter ^[ \t]*$\r?\n into find what, leave replace empty. This will match all lines starting with white space and ending with carriage return (in this case a windows crlf)
Click the Find Next button to see for yourself how it matches only empty lines.
Press ctrl + h (Shortcut for replace).
In the Find what zone, type ^\R ( for exact empty lines) or ^\h*\R ( for empty lines with blanks, only).
Leave the Replace with zone empty.
Check the Wrap around option.
Select the Regular expression search mode.
Click on the Replace All button.
You can follow the technique as shown in the following screenshot:
Find what: ^\r\n
Replace with: keep this empty
Search Mode: Regular expression
Wrap around: selected
NOTE: for *nix files just find by \n
This worked for me:
Press ctrl + h (Shortcut for replace)
Write one of the following regex in find what box.
[\n\r]+$ or ^[\n\r]+
Leave Replace with box blank
In Search Mode, select Regex
Click on Replace All
Done!
In notepad++ press CTRL+H , in search mode click on the "Extended (\n, \r, \t ...)" radio button then type in the "Find what" box: \r\n (short for CR LF) and leave the "Replace with" box empty..
Finally hit replace all
Well I'm not sure about the regex or your situation..
How about CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines and viola all blank line gone.
A side note - if the line is blank i.e. does not contain spaces, this will work
1) Ctrl + H ( Or Search 🠆 Replace..) to open Replace window.
2) Select 'Search Mode' 'Regular expression'
3) In 'Find What' type ^(\s*)(.*)(\s*)$ & in 'Replace With' type \2
^ - Matches start of line character
(\s*) - Matches empty space characters
(.*) - Matches any characters
(\s*) - Matches empty spaces characters
$ - Matches end of line character
\2 - Denotes the matching contend of the 2nd bracket
Refer https://www.rexegg.com/regex-quickstart.html for more on regex.
You can search for the following regex: ^(?:[\t ]*(?:\r?\n|\r))+ and replace it with empty field
Ctrl+H.
find - \r\r
replace with - \r.
This obviously does not work if the blank lines contain tabs or blanks. Many web pages (e.g. http://www.guardian.co.uk/) contain these white lines, as a result of a faulty HTML editor.
Remove white space using regular expression as follows:
change pattern: [\t ]+$
into nothing.
where [\t ] matches either tab or space. '+' matches one or more occurrences, and '$' marks the end of line.
Then use notepad++/textFX to remove single or extra empty lines.
Be sure that these blank lines are not significant in the given context.
Edit >> Blank Operations >> Trim Leading and Trailing Spaces (to remove black tabs and spaces in empty lines)
Ctrl + H to get replace window and replace pattern: ^\r\n with nothing (select regular expression)
Note: step 1 will remove your code intendation done via tabs and blank spaces
Sometimes \n\r etc not work, here to figure it out, what your actually regular expression should be.
Advantage of this trick: If you want to replace in multiple file at once, you must need this method. Above will not work...
CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines as suggested above works.
But if lines contains some space, then move the cursor to that line and do a CTRL + H. The "Find what:" sec will show the blank space and in the "Replace with" section, leave it blank.
Now all the spaces are removed and now try CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines
/n/r assumes a specific type of line break. To target any blank line you could also use:
^$
This says - any line that begins and then ends with nothing between. This is more of a catch-all. Replace with the same empty string.
I did not see the combined one as answer, so search for ^\s+$ and replace by {nothing}
^\s+$ means
^ start of line
\s+ Matches minimum one whitespace character (spaces, tabs, line breaks)
$ until end of line
This pattern is tested in Notepad++ v8.1.1
It replaces all spaces/tabs/blank lines before and after each row of text.
It shouldn't mess with anything in the middle of the text.
Find: ^(\s|\t)+|(\s|\t)+$
Replace: leave this blank
Before:
_____________________________________
\tWORD\r\n
\r\n
\tWORD\s\tWORD\s\t\r\n
\r\n
\r\n
WORD\s\s\tWORD\t\sWORD\s\r\n
\t\r\n
\s\s\s\r\n
WORD\s\sWORD\s\s\t\r\n
____________________________________
After:
_____________________________________
WORD\r\n
WORD\s\tWORD\r\n
WORD\s\s\tWORD\t\sWORD\r\n
WORD\s\sWORD
_____________________________________
A few of the above expressions and extended expressions did not work for me, but the regular expression "$\n$" did.
An easy alternative for removing white space from empty lines:
TextFX>TextFX Edit> Trim Trailing Spaces
This will remove all trailing spaces, including trailing spaces in blank lines.
Make sure, no trailing spaces are significant.
this work for me:
SEARCH:^\r
REPLACE: (empty)