Tuesday, March 7, 2017

Re: Ambiguity about what $ means?

On Mon, Mar 6, 2017 at 9:20 PM, Nikolay Aleksandrovich Pavlov
<zyx.vim@gmail.com> wrote:
[...]
> For reference: Vim documentation names such things "zero-width match"
> (`:h /zero-width`). `\n` may match a character if you use it in
> functions like `substitute()` (where pattern applies to one of the
> string arguments), but when searching a buffer this is some kind of
> pseudocharacter:
>
> 1. not zero-width because you can join lines using `s/\n//`,
> 2. also matches at the very end of the buffer even if `&endofline` is
> set to zero (designating that last line in the buffer should not end
> with a LF/CRLF/CR)
> 3. matches line end always, regardless of &fileformat setting (meaning
> that "physically" file may contain \n, \r\n or \r when being written)
> 4. internally there are no characters corresponding to a line end:
> each line is a NUL-terminating C string.

Well said.
In short:
• $ (a zero-width pattern atom) matches the end of a line at the end
of a pattern (or before \| etc.)
• \n matches an end-of-line anywhere in a pattern, regardless of how
that end-of-line is represented. (Internally all lines have an
end-of-line, which IIUC is the null at the end of a C string; 'noeol'
is only relevant when writing a 'binary' or 'nofixeol' file.)
• But to _insert_ a line break by means of a :substitute command, we
must use \r not \n so the following is one of several possible ways to
count lines (between two marks, in this example) without changing
them:

:'a,'b-1s/\n/\r/

Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment