3

I'm wanting to write a simple plugin that allows using a motion to choose some text and then processes that text. I'm trying to find out how to get the text that a motion results in. So far I haven't noticed anything under :help functions or :help motions that I might be able to use. I tried going through the source of vim-fireplace, but rapidly got lost.

So does anybody have a simple example or a link to a near trivial plugin that does something similar?

4

In short, you set 'operatorfunc' to point to the name of a function, then write a mapping that uses the g@ command.

The g@ command will take a motion (in operator-pending mode) and then will call the function set in 'operatorfunc', passing it an argument to indicate whether the motion was char-wise or line-wise. Additionally, it will set the '[ and '] markers pointing to the start and end of the motion.

See section Mapping an Operator in Vim help (also accessible through :help :map-operator) which will have some more details on that and an example. It will also have pointers to the documentation on g@ and 'operatorfunc'.

You might also want to read the Grep Operator, Part 2 section of the excellent Learn Vimscript the Hard Way. Actually, you should probably read the whole thing, if Vimscript interests you, but that particular section handles this very specific case of creating a mapping that takes a motion in operator-pending mode.

You might also want to make your mapping work in Visual mode. Both the Vim help and "Learning Vimscript the Hard Way" show examples of writing a function that will work for both, and the appropriate mappings to go along with that.

| improve this answer | |

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.