0

Say I have hierarchical markup like this:

<div id="level_1">
  <div id="level_2">
    <div id="level_3">
      watermelon
    </div>
  </div>
</div>

I can put my cursor on <div id="level_1"> and hit % to jump to the corresponding closing </div> tag. I can also visually select inside the block with vit or around the block with vat.

Say my cursor is on the text "watermelon" and I hit vat. I've now selected/highlighted the block starting from <div id="level_3"> through its ending tag:

  <div id="level_1">
    <div id="level_2">
######<div id="level_3">######### highlighted ######
########watermelon############### highlighted ######
######</div>##################### highlighted ######
    </div>
  </div>

How can I now expand the visual selection / go out one level so that I've now selected around <div id="level_2">?

  <div id="level_1">
####<div id="level_2">########### highlighted ######
######<div id="level_3">######### highlighted ######
########watermelon############### highlighted ######
######</div>##################### highlighted ######
####</div>####################### highlighted ######
  </div>

Once I've done that, how can I come back down a level to where I started?

(HTML is just an example here; answers should apply to any programming language / markup.)

1
  • 2
    Well, for HTML: what happens if you keep pressing at in visual mode?
    – D. Ben Knoble
    Commented Mar 10 at 0:43

1 Answer 1

3

When using "a something" text objects (at, a(, etc.) in visual mode, you can generally keep using them to expand the visual selection. In this case, you should be able to do vatat to select the parent element. You could even alternate between at and it if you want.

vat:

vat

One more at:

at

One last at:

at

But, and it is one of their limitations when used in visual mode, text objects always grow. Once you have selected "an" element, the only ways to select an inner element are to:

  • start over, by leaving visual mode, re-positioning the cursor, and re-entering visual mode with the correct text object,
  • adjust the boundaries of the visual selection with :help v_o and various motions.

FWIW, I am convinced that this is more an implementation issue than a conceptual limit.

You are not the first to ask for that kind of thing.

Your Answer

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

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