How do I replace a newline in Atom?

2019-03-09 08:53发布

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?

5条回答
\"骚年 ilove
2楼-- · 2019-03-09 09:32

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

查看更多
够拽才男人
3楼-- · 2019-03-09 09:36

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

查看更多
放荡不羁爱自由
4楼-- · 2019-03-09 09:41

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.
查看更多
Ridiculous、
5楼-- · 2019-03-09 09:46

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

查看更多
【Aperson】
6楼-- · 2019-03-09 09:55

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.

查看更多
登录 后发表回答