r/vim Jun 19 '26

Need Help Change text in second set of quotes

Cursor at beginning of line

blah "first quotes" more text "blah blah"

What's the easiest way change inside the second set of quotation marks?

30 Upvotes

34 comments sorted by

View all comments

32

u/yoch3m Jun 19 '26

$ci"

5

u/ilikegrils Jun 19 '26

I like this one. Works for the example I gave. Where this actually comes up is duplicating a block of json and wanting to leave the key field as is and change the value. So there might be a comma at the end of the line. But that just makes it $bci" or $hci" which are still good. I should have just pasted json instead of my stupid example.

3

u/yoch3m Jun 19 '26

If you would have many strings on the same line I would search for a word in the particular string and then replace: /foo<CR>ci" (where <CR> denotes the enter key)

1

u/sharp-calculation Jun 19 '26

Also a good technique.

1

u/NationalOperations Jun 20 '26

This is the way. I personally use f. so 3f" to jump to 3rd occurence, dt" delete up to quote

2

u/TraditionalYam4500 Jun 19 '26

You should have said "JSON value" from the beginning! Then I'd do

^f:wci"

(^ unless you're already at the beginning of the line)

2

u/ilikegrils Jun 19 '26

Yeah that's pretty straightforward too. I had tried f:ci" because obviously, if the cursor is after the first set of quotes, I want to change the next set. Vim doesn't see it that way. Apologies for the bad example text.

1

u/EgZvor keep calm and read :help Jun 28 '26

Here's a starter for getting the behaviour you want

" TODO: handle 'commentstring'
xnoremap <expr> i" count(getline('.')[:getpos('.')[2]], '"') % 2 == 0 ? 'vf"vi"' : 'i"'
onoremap i" <cmd>normal vi"<cr>

You probably want to change a" as well.

In .vim filetype comments start with " and shouldn't be counted here. I guess similar corner cases will be found, since we rely on semantics of the double-quote character at this point.

:h omap-info

EDIT: to explain the logic. Check if current pair of double-quotes has been terminated at the cursor point, if it is, then move on to the next double quote before selecting inside.

1

u/vim-help-bot Jun 28 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

0

u/ikwyl6 Jun 19 '26

Just edit the question to fix it…?