How do I select the contents inside curly braces?
Answer
vi{
Explanation
The vi{ command visually selects everything inside the nearest pair of curly braces {}, without selecting the braces themselves. It combines visual mode (v) with the inner braces text object (i{).
How it works
venters visual modei{is the "inner braces" text object — it selects all content between{and}, excluding the braces- Vim finds the nearest enclosing brace pair from the cursor position, even if the braces are on different lines
Example
Given the text with the cursor anywhere inside the braces:
if (true) {
console.log("hello");
console.log("world");
}
Pressing vi{ selects the two console.log lines (the content between { and }).
Tips
- Use
va{("a brace") to select the content and the surrounding braces - Use
vi}— it does exactly the same thing asvi{ - Use
di{to delete inside braces without entering visual mode - Use
ci{to change (delete and enter insert mode) the contents inside braces - Use
yi{to yank (copy) the contents inside braces - Works with nested braces — Vim finds the nearest enclosing pair from the cursor
- Similar text objects exist for other delimiters:
vi(for parentheses,vi[for brackets,vi"for double quotes,vi'for single quotes,vi<for angle brackets,vitfor HTML/XML tags - After selecting with
vi{, you can extend the selection outward by pressinga{to include the braces themselves