I am trying to write a custom command that executes the following: 'c""<Esc>P', namely cut visually selected text, insert ""and paste the cut text into the quotes.

I have tried the following:

command! -range Wq <line1>,<line2> execute 'c""<Esc>P'

and some other variations of it, but none seem to work. How can I write a custom command to execute this command?

I know about vim-surround but would like to write my own functions instead of some plugins.

(Also posted here: https://stackoverflow.com/questions/75820541/vim-custom-command-to-execute-cescp, got recommended to also post on vi SE)

1 Answer 1


I would do:

xnoremap Wq c""<Esc>P
  • 1
    Thanks! Also answered in stackoverflow.com/questions/75820541/… with xnoremap <key> c"<C-r>""<C-c>.
    – hilbor
    Mar 23 at 12:07
  • Welcome to Vim! Thanks for the feedback :-) Mar 23 at 12:12
  • This answer would be better with an explanation of why the command/Ex version is wrong. (And yea, I prefer xnoremap, too.)
    – D. Ben Knoble
    Mar 24 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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