Is it possible to define a custom movement in vim?

3

3

Is it even possible?

What I mean is, many vim commands can take a movement to operate on, can I define a custom movement so that I can tailor my edits to an environment?

The specific command I'm interested in is gq, to re-format. I can type gqip to re-format a paragraph, ip being the movement from the beginning to the end of the current paragraph excluding surrounding whitespace.

Is it possible to define a custom movement to replace ip to allow me to, say, re-format comments without touching surrounding code? This would require a movement similar to ip but with different conditions for where it starts or ends.

SpoonMeiser

Posted 2010-11-04T10:46:45.743

Reputation: 163

Answers

4

Take a look at this Vim Tips Wiki article on creating new text objects, and also at these two Vim plugins that provide functions for creating custom text objects and an example of creating custom text objects.

garyjohn

Posted 2010-11-04T10:46:45.743

Reputation: 29 085

You should probably expand this answer to briefly cover what those links mention. In particular, that it's omap that does what I want. – SpoonMeiser – 2010-11-06T01:29:42.710

@SpoonMeiser: I would if I understood it better myself. As it is, I just knew where to find more information. – garyjohn – 2010-11-06T03:52:17.180