← back to latest

Change a quoted string without moving the cursor to it

NORMALci"Change a quoted string without moving the cursor to it

anatomy

c
Change. Deletes whatever the following text object covers and drops you into insert mode.
i"
Inner double-quote text object. Selects the text between a pair of double quotes, leaving the quotes in place. If the cursor is not inside a quoted string, vim searches forward on the same line for the first one.

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 url in log("fetching", url, "with retries"), vim treats ", url, " as a quoted string, and ci" 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, not say \. 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, use ci'.

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.

lineage

Text objects arrived in Vim 5.0 in 1998, starting with words, sentences, paragraphs, and brackets. The quote objects i", i', and i` came later, in Vim 7.0 in 2006, and they were built differently from the rest: quotes do not nest, so vim cannot search outward for an enclosing pair the way it does for parentheses. It scans the current line instead, which is where the forward-seeking behavior comes from.

-- INSERT --vim-hacks.com020-020-change-a-quoted-string-without-moving-to-it.html
utf-81,1All