What Vim command(s) can be used to quote/unquote words?

前端 未结 16 890
余生分开走
余生分开走 2020-12-12 08:36

How can I quickly quote/unquote words and change quoting (e.g. from \' to \") in Vim? I know about the surround.vim plugin, but I would like to use

16条回答
  •  既然无缘
    2020-12-12 09:16

    Adding Quotes

    I started using this quick and dirty function in my .vimrc:

    vnoremap q :call QuickWrap("'")
    vnoremap Q :call QuickWrap('"')
    
    function! QuickWrap(wrapper)
      let l:w = a:wrapper
      let l:inside_or_around = (&selection == 'exclusive') ? ('i') : ('a')
      normal `>
      execute "normal " . inside_or_around . escape(w, '\')
      normal `<
      execute "normal i" . escape(w, '\')
      normal `<
    endfunction
    

    So now, I visually select whatever I want (typically via viw - visually select inside word) in quotes and press Q for double quotes, or press q for single quotes.

    Removing Quotes

    vnoremap s :call StripWrap()
    
    function! StripWrap()
      normal `>x`

    I use vim-textobj-quotes so that vim treats quotes as a text objects. This means I can do vaq (visually select around quotes. This finds the nearest quotes and visually selects them. (This is optional, you can just do something like f"vww). Then I press s to strip the quotes from the selection.

    Changing Quotes

    KISS. I remove quotes then add quotes. For example, to replace single quotes with double quotes, I would perform the steps: 1. remove single quotes: vaqs, 2. add new quotes: vwQ.


    • http://vim.wikia.com/wiki/Wrap_a_visual_selection_in_an_HTML_tag code from here was modified and used as part of my answer
    • https://github.com/beloglazov/vim-textobj-quotes vim-text-obj-quotes

提交回复
热议问题