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

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

    how about this?

     :%s/\'/"/g
    
    0 讨论(0)
  • 2020-12-12 09:13

    The macro ways !

    1. press q and q for recording into q register (we use "q" as a shortcut to remember "quotes").

    2. press shift + b move cursor to front of current word

    3. press i type ' (a single quotes)

    4. press esc then press e to move to end of word

    5. press a then press ' again to surround the word with quotes.

    6. press esc to get into normal mode.

    7. finally press q to record it into q register.

    How to use

    1. Move cursor to a desired word.
    2. Press @q to surround a word with quotes.
    3. Press @@ if you want repeat it into another word.

    You can alter step 4 with anything you like {a line, a word until found some character, etc}.

    Make recorded macro persistent

    1. open .vimrc
    2. go to end of file
    3. change to insert mode. type this to make it persistent:
    let @q='ctrl + r ctrl + r q'
    1. save and quit

    2. open your files, go to some words

    3. now press @q

    if you do it correctly, magic things should appear in your words.

    You can apply this to other macros you loved.

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

    If you use the vim plugin https://github.com/tpope/vim-surround (or use VSCode Vim plugin, which comes with vim-surround pre-installed), its pretty convinient!

    add

    ysiw' // surround in word `'`
    

    drop

    ds' // drop surround `'`
    

    change

    cs'" // change surround from `'` to `"`
    

    It even works for html tags!

    cst<em> // change surround from current tag to `<em>`
    

    check out the readme on github for better examples

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

    Quote a word, using single quotes

    ciw'Ctrl+r"'

    It was easier for me to do it this way

    ciw '' Esc P
    
    0 讨论(0)
  • 2020-12-12 09:16

    Adding Quotes

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

    vnoremap q <esc>:call QuickWrap("'")<cr>
    vnoremap Q <esc>:call QuickWrap('"')<cr>
    
    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 <esc>:call StripWrap()<cr>
    
    function! StripWrap()
      normal `>x`<x
    endfunction
    

    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
    0 讨论(0)
  • 2020-12-12 09:16

    Here are some simple mappings that can be used to quote and unquote a word:

    " 'quote' a word
    nnoremap qw :silent! normal mpea'<Esc>bi'<Esc>`pl
    " double "quote" a word
    nnoremap qd :silent! normal mpea"<Esc>bi"<Esc>`pl
    " remove quotes from a word
    nnoremap wq :silent! normal mpeld bhd `ph<CR>
    
    0 讨论(0)
提交回复
热议问题