10

I don't understand the underscore motion in vim, at least 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 in the line. However, if I use the motion with y then it yanks the whole line (like yy or Y), but y^ does not yank the whole line, but just to the first non-whitespace character, exclusive, as you might expect.

So, what exactly is underscore supposed to do?

10

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

  • Hmmm, ok, then what is the difference between y2_ and y2+? – Tyler Durden 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
  • 2
    Why does the _ command exist – why would someone prefer it to +? – Aaron Thoma Aug 27 '17 at 14:11

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.