24

I'm a bit puzzeled as to why the underscore command _, which jumps to the beginning of the (COUNT - 1)th line below the cursor, gets its own key. When would I use this rather than + or ^?

1 Answer 1

23

Good question!

As a motion by itself, you're right, _ is not a particularly useful key. :h _

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

The key word here is "linewise". So if your goal is to just move the cursor, then ^, j, and + all get the job done, and there's not much point to _. But as an argument to an operator, _ is super powerful.

A lot of double-key shortcuts are actually just shortcuts for _. For example,

dd -> d_
cc -> c_
yy -> y_
Y  -> y_

etc. Because it's a linewise motion, it basically selects the entire line to be operated on regardless of where your cursor starts, whereas ^ just goes from your character to the first non-blank.

It helps me when thinking about linewise vs blockwise motions to imagine that I'm pressing either v or V right before the motion. So I imagine d^ to be equivalent to v^d and I imagine d_ to be equivalent to V_d or V^d. Of course, you can also override a motion to force it to be linewise or characterwise. So dv_ is actually exactly the same as d^ or v^d, and dVw is like Vwd which is exactly like d_.

The reason it is specifically the "count-1th" line is so that 1dd (which is really d1_) deletes one entire line, 2dd (which is really d2_) deletes 2 entire lines, 3dd deletes 3 entire lines, etc.

6
  • 2
    All right, but would I ever actually use it? dd is easier than d_ and 3yj is easier than 4y_.
    – Toothrot
    Apr 25, 2019 at 18:02
  • 6
    @toothrot If you were creating your own operator and wanted to operate on the next n lines.
    – DJMcMayhem
    Apr 25, 2019 at 18:07
  • 6
    @Toothrot I guess I want to clarify that a little bit. In day to day vim editing, no it's really not particularly useful. I've almost never used it either. The reason it's useful is because it is the definition of a lot of useful commands. dd might be more convenient than d_, but that's because dd is just a mapping to d_. And it's also useful in vimscript.
    – DJMcMayhem
    Apr 25, 2019 at 19:24
  • 10
    Some actions that act on a "motion" may not have other shortcuts. Something like "gU_" will uppercase everything on the current line, even if the cursor is not at the start of the line.
    – bmb
    Apr 25, 2019 at 23:08
  • 3
    @bmb What about gUU? I think it would be worthwhile to find an actual example where <operator>_ would be used.
    – Hotschke
    May 15, 2019 at 18:08

Your Answer

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

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