1

I'm wondering why d$ and v$d behave differently.

  • d$ deletes from the cursor till the end of the line excluding the carriage return where
  • v$d delete from the cursor till end end of the line including the carriage return merging them the current line with the next

e.g.:

This.is.a.fox$
     ^
This.is.a.rabbit$

Where:

  • ^ mark the position of the cursor
  • . stand for a white space
  • $ stand for the carriage return

After d$

This.$
    ^
This.is.a.rabbit$

After v$d

This.This.is.a.rabbit$
     ^

The $ motion is marked inclusive from my understanding it means that the two operation should have the same result (that could be one of the two depending if we consider that the carriage return is part of the move or not).

One way to solve that contradiction is to say that the $ motion is exclusive and that it goes on the carriage return what it seems to do in visual mode but clearly does not in normal mode since if you combine it with i you end up inserting one character before the last one.

It seems to me that the $ motion depend on the mode:

In normal mode:

This.is.a.fox$
     ^-------
This.is.a.rabbit$

In visual mode:

This.is.a.fox$
     ^--------
This.is.a.rabbit$

What is the correct interpretation?

4
  • most likely it's related to because visual mode forces all motions to be "inclusive" from vi.stackexchange.com/questions/44054/…
    – d.k
    Commented Feb 3 at 13:17
  • 1
    Yes, that's why.
    – romainl
    Commented Feb 3 at 13:18
  • But the doc says that the $ is already inclusive. How could it then be more inclusive? Further more, as explained in the question, if we would consider $ exclusive (suppose the doc is imprécise or wrong) the $ix should insert x at the end of the line which it does not:-| Commented Feb 3 at 13:28
  • 1
    Sorry, I meant "that's because of visual mode", not "because visual mode forces inclusiveness".
    – romainl
    Commented Feb 3 at 13:51

1 Answer 1

2

In the context of :help $:

To the end of the line.  When a count is given also go
[count - 1] lines downward, or as far is possible.
|inclusive| motion.  [...]

"To the end of the line" really means "to the last printable character or whitespace on the line", not "to the line separator". The line separator, whatever it is, is not included:

      v                               " cursor before $
      ---------------------           " text covered by the motion
                          v           " cursor after $
Lorem ipsum dolor sit amet.$

That is the key point, here: $ is "inclusive", yes, but it doesn't include the line separator.

Because $ is "inclusive", using it after an operator covers all the text from the cursor (included) to the end of the line (included):

      v                               " cursor before d$
      ---------------------           " text covered by the motion
                          v           " cursor after d$
Lorem ipsum dolor sit amet.$

But :help $ doesn't stop there:

[...]
In Visual mode the cursor goes to just after the last
character in the line.
[...]

Keeping up with the example above, expanding the visual selection with $ effectively covers the actual line separator:

      v                               " cursor before v$
      ----------------------          " visually selected text
                           v          " cursor after v$
Lorem ipsum dolor sit amet.$

which means that d is going to delete all the rest of the line, including the line separator, effectively "joining" the line below with the text before the cursor:

      v                               " cursor before v$
      ----------------------          " visually selected text
                           v          " cursor after v$
Lorem ipsum dolor sit amet.$
Lorem ipsum dolor sit amet.$
Lorem Lorem ipsum dolor sit amet.$    " line after d
5
  • Your are so right. I should have read the documention till the end :-) Commented Feb 3 at 13:54
  • Do you know why it has been decided that the $ motion should behave differently in Normal and in Visual mode? Commented Feb 3 at 15:00
  • No. AFAIK many exceptions are justified by real world usage or "this makes more sense that way in this specific context" type of thinking. That said, there might be some kind of confirmation bias somewhere but I have found the documented behavior to be a pain in the ass more often than not, having to do <left> after v$ to have the correct visual selection.
    – romainl
    Commented Feb 3 at 15:30
  • I'm use to do vg_ or vgh instead of v$. I'm considering adding vnoremap $ $h. Do you know if there are good reason not to add that mapping? Commented Feb 3 at 15:47
  • 1
    Other than the pedantic "use x instead of v"? No.
    – romainl
    Commented Feb 3 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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