4

In Vim, v_ selects text from the current position to the actual begin of the line. Example:

Test test
   Test te|st

Now pressing v_ we get the selection:

Test test
   [Test te]st

From this I would expect, that d_ deletes the same characters instand of selecting it. But it turns out, that d_ deleted the whole line, like dd would.

My question is: why?

  • 2
    See :h _. In particular: linewise. – muru Apr 6 '16 at 12:24
  • 1
    I think that the intended way to select to the start of the line is use ^ or 0 depending on what start of the line means. – muru Apr 6 '16 at 12:31
  • 6
    @BorisBrodski As @muru said, the reason is that _ is a linewise motion, not characterwise. It selects entire lines. However, when you use a motion as a text object with an operator, you can temporarily force it to be characterwise by prefixing it with v. So, if you really wanted to delete from the cursor until the first non whitespace using the _ motion, you could type dv_. – user9433424 Apr 6 '16 at 12:37
  • 2
    @BorisBrodski: About ^ which isn't easily accessible see :h 'langmap'. That can be useful. – statox Apr 6 '16 at 13:23
  • 1
    Or @BorisBrodski you can nnoremap ^ to a more accessible key. – Cometsong Apr 6 '16 at 19:42
3

From :help linewise:

Generally, motions that move between lines affect lines
(are linewise), and motions that move within a line affect characters (are
characterwise).

_ is a motion which can move between lines. For example, 2_ will move the cursor on the first non whitespace of the line below the current one. So, there's a good chance for _ to affect whole lines when preceded by an operator, which is confirmed by :help _:

_  <underscore>     [count] - 1 lines downward, on the first non-blank
                    character linewise.

As explained in :help o_v, by typing v after an operator, you can still force the latter to use a linewise motion as a characterwise one instead:

v       When used after an operator, before the motion command: Force
        the operator to work characterwise, also when the motion is
        linewise.

So, if you wanted to delete from the cursor back to the first non whitespace on the line using the _ motion, you could type dv_.

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.