In the buffer
Before:
def connect(retries):
log.info("fetching config", retries)
^
return client.get("/api/v1/config")
After typing ci", then loading settings, then <Esc>:
def connect(retries):
log.info("loading settings", retries)
^
return client.get("/api/v1/config")
The cursor started at the beginning of the line, nowhere near a quote. ci" found the first quoted string to the right, emptied it, and left the quotes where they were.
When you would reach for it
You need to change a log message, a URL, a dictionary key, or a test fixture, and the string sits in the middle of a line full of other syntax. The usual route is f" to reach the opening quote, l to step inside, then ct" to change up to the closing one. ci" does all of that in three keys and does not care where on the line you are standing, as long as the string is at or to the right of the cursor.
Gotchas
- The search only goes forward, and only on the current line. With the cursor after the last quote on the line,
ci"does nothing at all. It never looks at the next line, so multi-line strings are out of reach. - Vim works out which quotes pair up by counting from the start of the line. With the cursor on
urlinlog("fetching", url, "with retries"), vim treats", url, "as a quoted string, andci"replaces, url,with your text. Put the cursor on or inside the string you mean, or before the first quote on the line. - Backslash-escaped quotes are skipped. In
"say \"hi\" to them",ci"from the start of the line changes the whole string, notsay \. The escape character comes from the'quoteescape'option, which defaults to a backslash. - The quote objects do not nest.
ci"inside"outer 'inner' text"changes the double-quoted string. For the single-quoted part, useci'.
Variants
da"
Deletes the string along with its quotes. a" also takes the whitespace after the closing quote, or the whitespace before the opening quote if there is none after. On "b" in f("a", "b", "c") that leaves f("a",, "c"), so one stray comma is left to clean up.
yi"
Yanks the contents of the next string without moving anything. Paste it elsewhere with p, or into the command line with Ctrl-r ".
ci'
ci`
The same object for single quotes and backticks. Both search forward on the line the same way.