2

Can the caret ^ and the sequence \%V be used to match the beginning of a line in a visual selection? And if not, what is the rationale?

The following example seems to indicate that ^ and \%V are not compatible, suggesting that visual selections do not include the beginning of the line, even if they are visual line selections that cover the whole line (i.e., selections started with V). Consider a simple substitution command with the n flag to count the number of blank lines in a visual selection.

  • :'<,'>s/\%V^$//n does not find any matches.
  • :'<,'>s/^$//n finds every blank line in the visual selection.

Is this the expected behaviour? I find it a bit counter intuitive.

1
  • 2
    The visual selection can only come after the start of line. So use s/^\%V/.. Apr 20 at 5:54
3

From :h /^

At beginning of pattern or after "|", "(", "%(" or "\n": matches start-of-line; at other positions, matches literal '^'.

Hence, as @ChristianBrabandt noted in comments, you must write your pattern as :'<,'>s/^\%V$//n, or alternatively, :'<,'>s/\%V\_^$//n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.