3

I am a big fan of text objects, and I like their composability. For example, if I have

\label{sec:this-is-a-section}

I can easily copy the sec:this-is-a-section part by navigating inside, and pressing yi} and use this, for example, to paste it after a \ref{ somewhere else.

What I am missing is a good way to jump between these two. Intuitively, I want a command that takes a text object (i}) selects this and searches for it. Does that exist, or is there a succinct way to achieve this?

There is such a thing for the specific case of the iw text object, namely the * command. So in a way I wonder if there is a generalization of this.

I am not looking for ways to jump between \label and \ref, e.g. with a tag file; these are just example for the purpose of the question.

4
  • 1
    How about pasting on the commandline? I.e. yank your object (yi}), and then press /<Ctrl-r>=<Enter> to search for it. Oct 28, 2015 at 10:12
  • From reading stackoverflow.com/a/3997110/946226 I would expect "/yi} to do something along these lines, but unfortunately, it cannot be used with y and p, although I wonder why. Oct 28, 2015 at 12:20
  • @PhilippFrank, it seems your suggestion is the best (if one wants a solution that works without scripting/plugins); would you mind turning it into an answer? Oct 28, 2015 at 19:53
  • Not at all. I'll also fix the register name :) Oct 29, 2015 at 11:31

2 Answers 2

1

Pasting in commandline-mode from a register is done via <C-r>(see :h <C-r>, this also works in insert mode). So you could simply yank your text object, be it via yi} or visual selection, and then type /<C-r>"<Enter> to search for it.

2
  • Quite nice, although it probably breaks down if the selected text contains special regex characters. Oct 29, 2015 at 13:20
  • True. You might get quite a long way by using \V (:h \V, the very-nomagic switch) before pressing <C-r> though. Oct 29, 2015 at 13:55
2

The visual-star-search plugin can be used to search for a visual block. It solves your problem: select an object the usual way, then press *.

4
  • I am reluctant to install plugins, but it is a good start. Oct 28, 2015 at 12:12
  • @JoachimBreitner Then you're missing the useful implementations of objects. But visual-star-search has 17 lines, you can copy the relevant parts to your config. Oct 28, 2015 at 15:13
  • Copying to the config is even worse. I, as far as possible, try to make use of plain vim in order to be able to use other computers without hassle. But that’s getting OT :-) Oct 28, 2015 at 19:12
  • Then re-type the commands to the config. The plugin consist of ~5 "plain Vim" commands. It isn't like there are 216 other ways of doing it, you know. :) Also, the advantage of installing the ~5 commands via a plugin is that somebody else is taking care of collecting bug reports, and fixing bugs. Oct 28, 2015 at 19:21

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.