search

Home  >  Q&A  >  body text

vim - How to quickly select a word and surround it with a special character while developing a program?

For example, there is a word

dcustmsg

I have selected the word in v mode. Is there any shortcut key to add quotation marks on both sides of the word to make it

"dcustmsg"
怪我咯怪我咯2773 days ago888

reply all(3)I'll reply

  • 黄舟

    黄舟2017-05-16 16:44:14

    The configuration file adds functions and key mappings as follows:

    function! s:surround()
        let word = expand("<cword>")
        let wrap= input("wrap with: ")
        let command = "s/".word."/".wrap.word.wrap."/"
        execute command
    endfunction
    nmap cx :call <SID>surround()<CR>

    In this way, place the cursor on the word in normal mode, and then use the shortcut key cx. It will prompt the characters to be filled on both sides. Just enter the characters and press Enter.
    For details, please refer to: How to repeatedly add text on both si...

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:44:14

    You can write a vim macro by yourself
    In normal mode, the cursor is placed in the middle of the word
    bi"<ESC>ea"<ESC>
    Just record the above paragraph as a macro and use it.

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 16:44:14

    diwi"<C-r>0<Esc> #Cut the string and copy it into double quotes...

    reply
    0
  • Cancelreply