2

Is it possible to create a smart text object for i), i], i}, i>, i' and i"?

The text object would represent the first innermost object it can find. For example, with cursor on the plus sign viv would select a + b below:

{ [ (a + b) ] }
2
  • 1
    I suppose you could try them and take the one with the shortest non-zero length, but it might not be perfect (and it could easily be slow)
    – D. Ben Knoble
    Sep 22, 2020 at 0:56
  • 1
    Welcome to Vi and Vim!
    – filbranden
    Sep 22, 2020 at 1:20

1 Answer 1

2

The targets.vim plug-in implements something very similar to what you described, the main difference being that it has two separate text objects one for "any block" (ib, etc.) which works for (), [] and {} and another one for "any quote" (iq, etc.) which works for single, double or back quotes.

See the README section on Multi Text Objects for more details.

The main reason for separate objects for blocks and quotes is that blocks typically nest, while quotes do not.

In any case, the very existence of this plug-in that implements a text object which will match one of several delimiters based on context should demonstrate that it's definitely possible to implement a smart text object. If you really want one that matches either blocks and quotes, you might be able to draw on the ideas of this plug-in in implementing your operator.

4
  • 1
    It makes sense to separate the blocks and quotes because of nesting. I'm using targets.vim and it fulfills my needs. I can now be less specific (ahem, more lazy) about keystrokes to work with blocks or quotes. Sep 22, 2020 at 2:20
  • 1
    Something really excellent about targets.vim is that it will jump to the next / previous text object when outside a block. So handy. Sep 22, 2020 at 2:27
  • 1
    Glad you liked targets.vim, @Nathanael! Check out the other neat features of that plug-in, such as ia (etc.) for arguments and the n for "next" and l for "last" (previous) match. It's a really handy plug-in to have around and learn to use well!
    – filbranden
    Sep 22, 2020 at 2:27
  • 1
    Thanks, @filbranden. I see that n and l on the cheatsheet and am playing around with it now. Love this plugin. Sep 22, 2020 at 2:28

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.