13

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?

15

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 -.

4
  • Hmmm, ok, then what is the difference between y2_ and y2+? Sep 16 '14 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
    Sep 16 '14 at 20:35
  • 4
    Why does the _ command exist – why would someone prefer it to +? Aug 27 '17 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. Jul 11 '19 at 1:01

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.