15

I do not understand the underscore motion in vim, at least not in the context of yank (y). My cheat sheet reads:

"soft" bol down

which I do not comprehend. If I use the motion alone, it seems to be the same as ^ which means: go to the first non-whitespace on the line.

However, if I use the motion with y then it yanks the whole line (like yy or Y). On the other hand, y^ does not yank the whole line, but only up to the first non-whitespace character, exclusive — as you might expect.

So, what exactly is the underscore _ motion supposed to do?

1 Answer 1

17

Without a count, ^ and _ are indeed equivalent, but the latter supports a count:

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

The linewise explains your second observation: when used as a motion, it not just covers the text between the previous position and the new one, but the whole set of lines covered.

Carefully reading the :help provides these insights. You'll also see (by proximity), that the _ command is closely related to + and -.

6
  • Hmmm, ok, then what is the difference between y2_ and y2+? Commented Sep 16, 2014 at 19:56
  • 2
    @TylerDurden The difference is 1 (line), as is the difference between [count] - 1 and [count] for any value of [count]. As Ingo mentioned, it would do you some good to read vim's built-in help which is quite clear.
    – jw013
    Commented Sep 16, 2014 at 20:35
  • 4
    Why does the _ command exist – why would someone prefer it to +? Commented Aug 27, 2017 at 14:11
  • Not to argue, but the help is a bit confusing on this one. [count] - 1 lines downward -- to me, it read "count: 1 lines downward" instead of the correct interpretation. I know, doesn't even make a lot of sense, but there's a little room for confusion there. Commented Jul 11, 2019 at 1:01
  • 1
    @estibordo Yes, that's the linewise default of the _ motion that I've mentioned. To make c_ equivalent to c^, you'd have to force characterwise: cv_ Commented Apr 3 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .