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 cursor2i(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(deletesx(innermost parens)d2i(deletescheck(x) || flag(second level)d3i(deletescondition && (check(x) || flag)(outermost)
Tips
- Combine with
cto 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