tpope / vim-surround

surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
https://www.vim.org/scripts/script.php?script_id=1697
13.39k stars 444 forks source link

[Request] Swap in quotes #377

Closed jrocketfingers closed 1 year ago

jrocketfingers commented 1 year ago

A thing I find myself doing often is selecting an item that needs to be swapped into quotes or parentheses in a few places.

Doing a di" nukes the default yank register, so I need to do "_di"p, which isn't too bad, but it feels like it could be a thing of it's own. Something like "swap in quotes".

If this is too niche, I'd love to hear a way of constructing this through vim mappings.

tpope commented 1 year ago

I run into this too, although more often than a string it's a multi-line block of code. And seeing how it's not specific to strings/surroundings, I don't think it fits with surround.vim.

I'd love to hear a way of constructing this through vim mappings.

If the string case is the common one, you could literally map something to "_di"p. If you want to allow arbitrary targets, you could do it with an 'opfunc'. This wouldn't be that hard, but it's fiddly enough that I'm not gonna take the time to implement it myself.

tpope commented 1 year ago

Oh, from :help v_P, this sounds extremely close to what you want:

With |P| the unnamed register is not changed (and neither the selection or
clipboard), you can repeat the same change. But the deleted text cannot be
used.

So vi"P would do what you want. That's about as short as any general purpose map would be.

jrocketfingers commented 1 year ago

Awesome, it's not just the shortness, it's the "flow" of vi"P that doesn't require backtracking which makes it so nice. Thanks @tpope!