>  Q&A  >  본문

정규 표현식 - vim의 \n과 \r의 차이점은 무엇입니까

vim에서 줄바꿈 문자를 바꾸려면 n을 검색해야 하지만, 줄바꿈 문자로 바꾸려면 r입니다

으아악

아무 일도 일어나지 않습니다. 이론적으로는 모든 n을 r로 바꾸고 다시 검색할 수 있습니다

으아악

아직 아무것도 아니지만

으아악

가치가 있으니 원칙을 찾아주세요~~~

高洛峰高洛峰2712일 전857

모든 응답(1)나는 대답할 것이다

  • 我想大声告诉你

    我想大声告诉你2017-05-16 16:44:44

    전에 글을 썼는데 공개가 안되어 참고용으로 올립니다.

    (원문은 마크다운 형식이므로 SF에서 완전히 지원하지는 않지만 읽기에는 영향을 미치지 않습니다)

    메모 텍스트

    Vim의 줄 바꿈 문제
    ===============

    Vim에서 s/n/n/g를 사용하려면 파일의 모든 줄 바꿈이 ^@ 문자로 대체됩니다. code>s /n/n/g는 아무런 효과가 없어야 하지만 Vim에서는 n이 약간 다르게 처리됩니다. s/n/n/g 会导致文件的换行全部被替换成 ^@ 字符,按照通常的理解,s/n/n/g 不应该有任何的效果,但在 Vim 中,n 的处理略有不同。

    :help NL-used-for-Nul

    [<Nul>](http://vimdoc.sourceforge.net/htmldoc...) characters in the file are stored as [<NL>](http://vimdoc.sourceforge.net/htmldoc...) in memory. In the display they are shown as "^@". The translation is done when reading and writing files. To match a [<Nul>](http://vimdoc.sourceforge.net/htmldoc...) with a search pattern you can just enter [CTRL-@](http://www.vim.org/htmldoc/insert.htm...) or "[CTRL-V](http://www.vim.org/htmldoc/insert.htm...) 000". This is probably just what you expect. Internally the character is replaced with a [<NL>](http://vimdoc.sourceforge.net/htmldoc...) in the search pattern. What is unusual is that typing [CTRL-V](http://www.vim.org/htmldoc/insert.htm...) [CTRL-J](http://www.vim.org/htmldoc/insert.htm...) also inserts a [<NL>](http://vimdoc.sourceforge.net/htmldoc...), thus also searches for a [<Nul>](http://vimdoc.sourceforge.net/htmldoc...) in the file. {Vi cannot handle [<Nul>](http://vimdoc.sourceforge.net/htmldoc...) characters in the file at all}

    从这段说明可以了解到在 Vim 中,空字符 <Nul>(ASCII 0)在内存中是作为 <NL>(newline) 进行处理的。

    n matches an end-of-line
    When matching in a string instead of buffer text a literal newline character is matched.

    'n' 在搜索表达式中是在匹配一个字符串,所以字面意义的 'new line' 字符串会被匹配到,而不是 <Nul> 字符。在替换表达式中 'n' 会被解释为 <NL>,于是在内部,<Nul> 会被输入,所以在替换的表达式中 'n' 不再表示 'new line' 或者 'end-of-line'。同样,在搜索表达式中,输入 "CTRL-@" 或者 "CTRL-V 000" 表示 <Nul> 字符,但是在内部,他们还是都被替换为 <NL> 进行处理的,这也就是为什么直接键入 CTRL-V CTRL-J(输入的是 <NL>

    :Nul에 사용되는 NL을 도와주세요🎜 파일의
    [](http://vimdoc.sourceforge.net/htmldoc...) 문자는 [ >](http://vimdoc.sourceforge.net/htmldoc...) 화면에서는 파일을 읽고 쓸 때 "^@"로 표시됩니다. . [<Nul>](http://vimdoc.sourceforge.net/htmldoc...)를 검색 패턴과 일치시키려면 [CTRL-@](http:/ / www.vim.org/htmldoc/insert.htm...) 또는 "[CTRL-V](http://www.vim.org/htmldoc/insert.htm...) 000"입니다. 내부적으로 문자는 검색 패턴에서 [<NL>](http://vimdoc.sourceforge.net/htmldoc...)로 대체됩니다. [CTRL-V](http://www.vim.org/htmldoc/insert.htm...) [CTRL-J](http://www.vim.org/htmldoc/insert.htm...) 입력 )은 또한 [<NL>](http://vimdoc.sourceforge.net/htmldoc...)를 삽입하므로 [<Nul> ](http://vimdoc.sourceforge.net/htmldoc...) 파일에 있습니다. {Vi는 [<Nul>](http://vimdoc.sourceforge.net/을 처리할 수 없습니다. htmldoc ...) 파일의 모든 문자}
    🎜이 설명을 보면 Vim에서 null 문자 (ASCII 0)이 메모리에서 (newline)으로 처리된다는 것을 알 수 있습니다. . 🎜
    n은 줄 끝과 일치합니다.🎜버퍼 텍스트 대신 문자열에서 일치하면 리터럴 개행 문자가 일치됩니다.
    🎜'n'은 검색 표현식의 문자열과 일치하므로 <Nul> 문자가 아닌 리터럴 'new line' 문자열이 일치합니다. 대체 표현식의 'n'은 <NL>로 해석되므로 내부적으로는 <Nul>이 입력되므로 대체 표현식 'n'에는 no 더 길다는 것은 '새 줄' 또는 '줄 끝'을 의미합니다. 마찬가지로 검색 표현식에서 "CTRL-@" 또는 "CTRL-V 000"을 입력하면 <Nul> 문자가 표시되지만 내부적으로는 여전히 <NL> 가 처리되므로 CTRL-V CTRL-J를 직접 입력하면(입력은 <NL> 문자 자체임) 동일한 효과가 나타납니다. 🎜

    혼란의 원인은 Vi가 <Nul> 문자를 지원하지 않고 Vim이 Vi에서 개발되었기 때문일 수 있다고 생각합니다. NL>는 'NL-used-for-Nul'의 문자 그대로의 의미인 <Nul>을 나타내는 데 사용됩니다. 동시에 의 원래 기능은 문자 그대로 'CR-used-for-NL'의 의미인 로 대체됩니다. . <Nul> 字符,而 Vim 由 Vi 发展而来,作者可能为了方便,直接使用了 <NL> 用来表示 <Nul>,这也就是 'NL-used-for-Nul' 的字面意思。与此同时,<NL> 原本的作用则使用 <CR> 来替代,这也就是 'CR-used-for-NL' 的字面意思。

    :help CR-used-for-NL

    When 'fileformat' is "mac", <NL> characters in the file are stored as <CR> characters internally. In the text they are shown as "^J". Otherwise this works similar to the usage of <NL> for a <Nul>.
    When working with expression evaluation, a <NL> character in the pattern matches a <NL> in the string. The use of "n" (backslash n) to match a <NL> doesn't work there, it only works to match text in the buffer.

    那么,我们如何在替换表达式里表示 <NL> 呢?答案就是 <CR>,可以使用 CRTL-V CRTL-M(输入的是 <CR> 字符本身) 或者 'r',Vim 并不会在文件中直接输入 <CR> 字符,它会根据当前 ‘fileformat’ 的设置来决定使用 <CR>(Mac),<NL>(*nix) 还是 <CR><NL>

    :NL에 사용되는 CR을 도와주세요

    'fileformat'이 "mac"인 경우 파일의 <NL> 문자는 내부적으로 "^J"로 표시됩니다. <Nul>에 대한 <NL>
    표현식 평가 작업 시 패턴의 <NL> 문자는 문자열의 <NL> <NL> 여기서는 작동하지 않으며 버퍼의 텍스트와 일치하는 경우에만 작동합니다.

    그렇다면 대체 표현식에서 <NL>를 어떻게 표현할까요? 대답은 입니다. CRTL-V CRTL-M(입력은 문자 자체임) 또는 'r'을 사용할 수 있습니다. Vim은 <CR> 문자를 파일에 직접 입력하지 않고 현재 설정에 따라 (Mac), 을 사용하기로 결정합니다. '파일 형식' 설정입니다. ;(*nix) 또는 <CR><NL>(dos).
    1. 참고자료
    2. --------
    3. [Vim: n 대 r](http://stackoverflow.com/questions/35...)
    4. [왜 r은 Vim의 개행 문자인가요?](http://stackoverflow.com/questions/71...)
    [Vim은 정규식을 사용하여 ","를 개행 문자로 바꿉니다](http://blog.longwin.com.tw/2008/08/vi...)🎜 🎜[Vim에서 개행문자를 어떻게 바꾸나요?](http://stackoverflow.com/questions/71...)🎜 🎜

    회신하다
    0
  • 취소회신하다