vimtricks.wiki Concise Vim tricks, one at a time.

How do I expand a visual selection to include the outer block?

Answer

vi{a{

Explanation

In visual mode, you can expand your selection to include outer nested blocks by pressing additional text object commands. Starting with vi{ and then pressing a{ expands to the enclosing block.

How it works

  • vi{ selects the inner content of the current {} block
  • While still in visual mode, pressing a{ expands to include the outer block
  • Continue pressing a{ to keep expanding outward

Example

function outer() {
    if (true) {
        return inner();
    }
}

With the cursor on return, vi{ selects the if block content. Then a{ expands to include the if's braces. Then a{ again expands to the function body.

Tips

  • Works with any paired text object: vi(a(a( for nested parens
  • vi" then a" expands from inner to around quotes
  • This is called "growing" the selection
  • Very useful for selecting increasingly large scopes

Next

How do you yank a single word into a named register?