How do I replace a newline in Atom?

落花浮王杯 提交于 2019-12-29 03:18:24

问题


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?


回答1:


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.



回答2:


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:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/31400866/how-do-i-replace-a-newline-in-atom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!