Lets say I have "string" and I want to dupplicate the double quotes. What is the shortest way to do it?.

I have done a search of double quotes using /". Then I navigate to the different occurences using n command and I apply this sequence of commands: xPP

I know this can be done also using these commands: xPp or xP.

Excluding macros and function definitions, is there a shorter key combination to achieve that?

  • 3
    What about :s/"/""/gc ? – Maxim Kim Jan 20 at 8:06
  • 1
    I would also do /" then s""<esc> then n., n., ... – Maxim Kim Jan 20 at 8:21

I would do:

  1. /" to search for the character
  2. yl to copy the character
  3. p to paste the character
  4. then alternate between n and . to search and replace
| improve this answer | |

If you don't mind using the plugin vim-surround it can be as short as ysa"":

  • Put your cursor on the word you want to surround
  • ys is the vim surround command
  • a" is the text object to operate on, it could also be aW
  • " is the character you want to use to surround the text object

And as a bonus you can to the reverse operation i.e removing the surrounding quotes with ds"

Without a plugin I would go for :s/"[^"]\+"/"&"

  • "[^"]\+" allows you to capture a quote followed by any number of characters other than " followed by "
  • "&" here & is a reference to the whole matched text which as to be surrounded by two quotes.
| improve this answer | |
  • 4
    There are so very few vim plugins i feel are essential (ie most of them accomplish something that can be done natively with a little effort, but make the task smoother). Vim-surround, while technically just smoothing it’s tasks, is one of these essential few for it’s “vimminess” and constant usefulness. – D. Ben Knoble Jan 20 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.