jose isaias cabrera wrote:

"A.J.Mechelynck" wrote,


Bill McCarthy wrote:
On Sat 27-Jan-07 11:15pm -0600, you wrote:

Ok, here is a question: Why not keep it the same, \n? Any reasoning behind
it?

That could be a way to do it, but that choice wasn't made.
Perhaps someone else could explain why \n means end-of-line
in the pattern, but nul in the substitution, while \r means
^M in the pattern, but end-of-line in the substitution?  And
why not a \x representation for nul in the pattern - instead
of needing to do a [EMAIL PROTECTED]

[Note:  ^M is created with ctrl-v following by <enter> and
^@ is created by ctrl-v followed by 000.]


IIUC, NULs are replaced by LF everywhere in Vim memory, because a NUL terminates a C string while a LF is the only ASCII character guaranteed not to exist (in Unix 'fileformat') in the middle of a line (as opposed to at its end). So if lines are stored as C strings, substituting NUL for LF and vice-versa allows representing everything. (Mac 'fileformat' may required some fancy footwork; Dos 'fileformat' is handled by treating either CR+LF or LF as a linebreak on input, and writing all linebreaks as CR+LF on output.)

Why not use the same character in "replace by" as in "search for" is harder to explain, though. Maybe part of it is for historical "compatibility" reasons, but it doesn't explain why that choice was made by whoever made it. It may have something to do with the fact that hitting <Enter> at the keyboard generates a CR but is stored (in memory) as a line break then (on file, if in Unix 'fileformat') as a LF.

One of the things I love about vim is that it makes sense. And I don't have to learn or memorize much when things make sense, I just follow the logic. But this question, "how do I replace with a newline", I have asked about 5 times now. Why? Because I remember most things with vim since working with vim makes sense. But this \n vs. \r makes no sense to me and I keep forgetting.

Perhaps, on the next big release, this command would be possible,

:%s/,/\n/g

and have commas be replaced by newline.

Just a thought...

Thanks for all the replies.

josé


It won't, because adding a "\n" or a ^J in the middle of a line gets translated into a NUL -- always. It has for as long as Vim has existed and maybe longer, and you know Bram's standing on upward compatibility (IMHO, rightly so). It's just one of those "Vim quirks" -- one of the few exceptions to the systematic rules -- that to count linebreaks by replacing them with themselves you have to use

        :[range]s/\n/\r/

It is documented, BTW: under
        :help /\n
        :help s<CR>
                including the few lines below the latter
...and maybe also elsewhere.


Best regards,
Tony.

Reply via email to