Region expanding

3.5
(206)

Vim built-in text objects concept is a powerful way to perform operations on more than a single character. Text objects are defined based upon the syntax of your text and provide context to Vim about what you are editing to make changes more efficient.

When you understand text objects, you can perform operations on them more quickly. For example, combining v for “visual select” with iw for “inside word” means that viw will select inside the word. Furthermore, vaw will select around the word, vap will select around a paragraph, and on and on.

But one plugin can really power up your efficiency around selecting text objects by incrementally expanding your selected text area. The vim-expand-region plugin will map the selection of any sized text object to a single key. This mimics Emac’s expand region capability, IntelliJ’s syntax aware selection, and Eclipse’s select enclosing element.

Out of the box, the plugin provides the following mappings:

  • + expand visual selection to next incremental text object
  • - shrink visual selection back one level

You can customize these mappings to anything you like. Personally, I use v and ctrl-v which means all I need to remember to select virtually any chunk of text is the letter v. A single press selects the letter, pressing again selects the word, then the paragraph. You can really supercharge your workflow by customizing the progression of text objects. Here’s a customization I use for Ruby which adds increments for selecting inside the method, and then around the method:

call expand_region#custom_text_objects('ruby', {
       \ 'im' :0,
       \ 'am' :0,
       \ })

The plugin README has numerous examples to support your custom selection progressions. For example, selecting word, line, method, in that order, making selection of an entire method just 3 taps of v.

Let’s watch a gif showing this in action. You can also watch the video version on our YouTube Channel.

How useful was this tip?

Average rating 3.5 / 5. Vote count: 206

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts