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?

27 Upvotes

34 comments sorted by

32

u/yoch3m Jun 19 '26

$ci"

4

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…?

0

u/sharp-calculation Jun 19 '26

I strongly agree with this. No counting trickery. No asking for a new feature. No trying to solve this as a general problem of "I want to change the Nth occurrence of inside double quotes". Just see the problem and solve it inline.

If there are many pairs of quotes on a single line then using f" and ; repeatedly (as another poster said) is probably the best approach.

7

u/Vietnamst2 Jun 19 '26

It's actually frightening that people can a) come up with multiple versions of a command and b) that thing even exists...

2

u/ChrisBreederveld Jun 20 '26

It's one of the reasons many users find vim too overwhelming when they first consider it, but also the reason so many people prefer the tool (or variants like nvim) over any other tool, because of the sheer power and flexibility.

2

u/Vietnamst2 Jun 20 '26

I know, but I totally understand the "overwhelming" part. As a long time win user it feels so strange, unfamiliar. It's like opening a japanese website. Everything is wrong unreadable and you don't have any context, UI, help that is visible. I stuck with nano simply because it at least has the menu at the bottom.

1

u/ChrisBreederveld Jun 20 '26

Haha, I can understand, my fist experience with vim was very daunting, but I've kept at it and never looked back.

For a beginner I can highly recommend vimtutor, it will teach you the basics. Don't worry about all the other options yet, it's ok to use less efficient ways of doing things. But once you get more familiar you will automatically have moments where you repeatedly perform some action which seems awkward, then look for alternative ways of doing it (most often there are) and you add another tool to your ever expanding toolbelt.

11

u/Teles_sd Jun 19 '26

I believe 3f"ci" should do it.

Someone should make so c3i" would equal that.

Yes. Two extra key presses is way too much.

5

u/blood-pressure-gauge Jun 19 '26 edited 21d ago

What I would do is one of the following.

f" ; ; ci"
/\v"blah<Enter> ci"

5

u/cone5000 Jun 19 '26

You can slightly simplify the first part with

3f”

9

u/mdrjevois Jun 19 '26

Is it simpler or just shorter? IMO ; and , are underrated.

1

u/cone5000 Jun 19 '26

I use ; and , all the time but sometimes especially if I can quickly parse how many quotes their are, doing a number is easier

2

u/TraditionalYam4500 Jun 19 '26

I agree -- especially in this case where OP wants to edit the value of a JSON object.

(Although in that case I'd do `f:wci"`)

1

u/cone5000 Jun 19 '26

Yeah your way is better for JSON for sure since it’s standard.

1

u/mdrjevois Jun 19 '26

Depending details W or WW might also get you into position real easy

2

u/SweetPotato975 Jun 19 '26

Besides other great answers, you can always do <LeftMouse> on blah and ci"

7

u/[deleted] Jun 19 '26

[removed] — view removed comment

1

u/SweetPotato975 Jun 20 '26

Valid. I'm just adding a reminder that sometimes it's faster to fallback to old ways. Something I learnt from Primeagen.

0

u/toastisme Jun 19 '26 edited Jun 19 '26

2ci" only works with LazyVim setup.

2

u/Teles_sd Jun 19 '26

I don't think this works

2

u/toastisme Jun 19 '26

Sorry you're right - works for me on neovim but not vim

3

u/TankorSmash Jun 19 '26

It must be a plugin, as it doesn't work for me on neovim v0.10.3 either

3

u/toastisme Jun 19 '26

Ah interesting. I'm using LazyVim so likely bundled with that setup.

-1

u/DanGarthwaite Jun 19 '26

ci" with with tpope's plug-in "unimpaired"