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

How do I target outer nested brackets using counted text objects?

Answer

d2i(

Explanation

Vim text objects accept a count prefix that lets you target outer layers of nested delimiters. The command d2i( deletes the contents of the second enclosing pair of parentheses, letting you break out of nested structures in a single motion.

How it works

  • i( targets the innermost parentheses around the cursor
  • 2i( targets the second level of enclosing parentheses
  • This works with any delimiter: 2i{, 2i[, 2i", etc.

Example

Given the text with cursor on x:

if (condition && (check(x) || flag))
  • di( deletes x (innermost parens)
  • d2i( deletes check(x) || flag (second level)
  • d3i( deletes condition && (check(x) || flag) (outermost)

Tips

  • Combine with c to change: c2i( clears the outer parentheses and enters insert mode
  • Works in visual mode too: v2i( visually selects the second enclosing pair
  • This technique is especially useful in deeply nested code like Lisp, JavaScript callbacks, or complex conditionals

Next

How do I always access my last yanked text regardless of deletes?