How do I change the content inside square brackets without deleting the brackets themselves?
Answer
ci[
Explanation
ci[ (change inside [) deletes the content between the nearest pair of square brackets and puts you in Insert mode. The brackets themselves are left intact. This is one of Vim's text-object operations — it targets the content between delimiters rather than requiring you to navigate manually.
How it works
ci[orci]— change inside square bracketsca[orca]— change around (including the brackets themselves)di[— delete inside bracketsyi[— yank inside bracketsvi[— visually select inside brackets
Supported bracket pairs
| Text object | Delimiters |
|---|---|
ci( / ci) |
Parentheses () |
ci[ / ci] |
Square brackets [] |
ci{ / ci} |
Curly braces {} |
ci< / ci> |
Angle brackets <> |
ci" |
Double quotes |
ci' |
Single quotes |
ci` |
Backticks |
Example
Given: myArray[index + 1]
With cursor anywhere on the line, ci[ deletes index + 1 and enters Insert mode inside the brackets — ready for you to type the replacement.
Given: import {useState, useEffect} from 'react'
ci{ deletes useState, useEffect and puts cursor inside the braces.
Tips
- The cursor can be anywhere on the line — Vim searches forward to find the nearest bracket pair
2ci(changes the content of the second level of parentheses out from the cursor- These work in Normal mode as operators and can be prefixed with counts
:help text-objectslists all built-in text objects