What is newline character — '\n'

前端 未结 8 2123
挽巷
挽巷 2020-12-07 01:18

This is a very basic concept, but something I have never been able to articulate that well. and I would like to try to spell it and see where I go wrong.

If I have t

8条回答
  •  执笔经年
    2020-12-07 01:56

    I see a lot of sed answers, but none for vim. To be fair, vim's treatment of newline characters is a little confusing. Search for \n but replace with \r. I recommend RTFM: :help pattern in general and :help NL-used-for-Nul in particular.

    To do what you want with a :substitute command,

    :%s/\_$/\r
    

    although I think most people would use something like

    :g/^/put=''
    

    for the same effect.

    Here is a way to find the answer for yourself. Run your file through xxd, which is part of the standard vim distribution.

    :%!xxd
    

    You get

    0000000: 4361 6c69 666f 726e 6961 0a4d 6173 7361  California.Massa
    0000010: 6368 7573 6574 7473 0a41 7269 7a6f 6e61  chusetts.Arizona
    0000020: 0a                                       .
    

    This shows that 46 is the hex code for C, 61 is the code for a, and so on. In particular, 0a (decimal 10) is the code for \n. Just for kicks, try

    :set ff=dos
    

    before filtering through xxd. You will see 0d0a (CRLF) as the line terminator.

    :help /\_$
    :help :g
    :help :put
    :help :!
    :help 23.4
    

提交回复
热议问题