2

Here's a situation I'm in every day:

0  .outer_selector {
1  
2   .inner_selector {
3     margin: 0;
4     padding: 0;
5
6     input { display: none; }
7   }
8
9  } 

Now I want to cut and paste the .inner_selector somewhere else. How can I do it? I can't figure out any way to quickly delete the logical .inner_selector block with it's surrounding whitespace; i.e., the equivalent to dap but for this brace-delimited block. Here are various ideas, in increasing order of gnarliness. (ai and ii mappings are via the vim-indent-obj plugin.)

  1. 3Gdai. This almost works but leaves the closing brace for the .inner_selector block where it is.

  2. 2Gdai. No good; this deletes the whole .outer_selector block.

  3. 2Gdii. This deletes the correct block, but not the surrounding whitespace (i.e., the equivalent of dip instead of dap).

  4. 2Gf{d%. Go to { with f{, delete to matching brace (d%). This deletes the text from the brace on line 2 to its matching brace, but leaves the text ".inner_selector".

  5. 2Gf{v%okd. Starts same as (4), then jump to top of selected text (o), extend by one line upward (k), and delete (d). Same as (3), above.

  6. 3Gvaikojjd. Select text, select one additional line upward (k), jump to bottom of selected region (o), select two lines down (jj), delete (d). This works.

I can define a keyboard mapping for the last one and be done with it, but before I do that I want to be sure I'm not missing something here. Since it's such a common code construct in many languages, I'm surprised it's this difficult to treat text { /* multiple lines /* } as a text object, or that there's no plugin for it.

1 Answer 1

1

There is a text object for braces, it's B or { or }.

If you go to the whitespace in .outer-selector but out of .inner-selector you can see it select everything with Vi{ to select everything inside the .outer-selector braces.

I can also hit just the .inner-selector parts by running Vi{ inside .inner-selector and then hitting V again to force it to take all the full lines. This won't get you the whitespace around .inner-selector but I thought it might be of interest as well.

1
  • 1
    Thanks Camilo! I didn't know about hitting V again to select the full line -- cool trick. (Though I only got it to work with va{V, not vi{V.) You're right, the first method works perfectly in my example; however, it won't work if I have multiple .inner_selectors and only want to delete one.
    – Sasgorilla
    Oct 30, 2015 at 15: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.