djsadinoff
djsadinoff

Reputation: 5612

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?

Upvotes: 57

Views: 92946

Answers (8)

harvyS
harvyS

Reputation: 648

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'.

Upvotes: 1

paakjis
paakjis

Reputation: 407

None of these answers helped me. What worked for me:

  1. I just added a new line at the end of the file.
  2. Shift + <- (arrow to left)
  3. Ctrl + C
  4. Ctrl + V in the "Replace in current buffer" line

Just copied the new line and pasted it in :D

Upvotes: 12

user3804598
user3804598

Reputation: 415

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

Upvotes: 0

SYNAIKIDO
SYNAIKIDO

Reputation: 79

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

Upvotes: 6

Alphan
Alphan

Reputation: 579

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

Upvotes: 47

espigel
espigel

Reputation: 31

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.

Upvotes: 3

pmrotule
pmrotule

Reputation: 9692

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).

Upvotes: 2

djsadinoff
djsadinoff

Reputation: 5612

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.

Upvotes: 53

Related Questions