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

How do I select all text inside square brackets?

Answer

vi[

Explanation

The vi[ command selects the text inside square brackets without including the brackets themselves. It works from any position inside the brackets.

How it works

  • v enters visual mode
  • i[ is the "inner square brackets" text object
  • Selects everything between [ and ]
  • Handles nested brackets correctly

Example

array[index + offset]

With the cursor inside the brackets, vi[ selects index + offset.

Tips

  • va[ includes the brackets in the selection
  • di[ deletes inside brackets
  • ci[ changes inside brackets
  • vi] is a synonym for vi[
  • Nest with other text objects: select inside nested brackets

Next

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