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

前端 未结 16 878
余生分开走
余生分开走 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:22

    For users of VSCodeVim you can do

    vwS"

    • You can replace " with whatever you would like to wrap by.
    • You can replace w with any other selection operator
    0 讨论(0)
  • 2020-12-12 09:23

    Visual mode map example to add single quotes around a selected block of text:

    :vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
    
    0 讨论(0)
  • 2020-12-12 09:30

    I don't know any builtin vim command for this, but using r"f'r" to change from ' to " and r'f"r' to change from " to ' works if you stand on the first ' or ". The command r' replaces whatever character is under your cursor with ', and f" moves you forward to the next ".

    0 讨论(0)
  • 2020-12-12 09:31

    In addition to the other commands, this will enclose all words in a line in double quotes (as per your comment)

    :s/\(\S\+\)/"\1"/
    

    or if you want to reduce the number of backslashes, you can put a \v (very-magic) modifier at the start of the pattern

    :s/\v(\S+)/"\1"/
    
    0 讨论(0)
提交回复
热议问题