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

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

    To wrap in single quotes (for example) ciw'<C-r>"'<esc> works, but repeat won't work. Try:

    ciw'<C-r><C-o>"'<esc>
    

    This puts the contents of the default register "literally". Now you can press . on any word to wrap it in quotes. To learn more see :h[elp] i_ctrl-r and more about text objects at :h text-objects

    Source: http://vimcasts.org/episodes/pasting-from-insert-mode/

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

    I wrote a script that does this:

    function! WrapSelect (front)
        "puts characters around the selected text.
        let l:front = a:front
        if (a:front == '[')
            let l:back = ']'
        elseif (a:front == '(')
            let l:back = ')'
        elseif (a:front == '{')
            let l:back = '}'
        elseif (a:front == '<')
            let l:back = '>'
        elseif (a:front =~ " ")
            let l:split = split(a:front)
            let l:back = l:split[1]
            let l:front = l:split[0]
        else
            let l:back = a:front
        endif
        "execute: concat all these strings. '.' means "concat without spaces"
        "norm means "run in normal mode and also be able to use \<C-x> characters"
        "gv means "get the previous visual selection back up"
        "c means "cut visual selection and go to insert mode"
        "\<C-R> means "insert the contents of a register. in this case, the
        "default register"
        execute 'norm! gvc' . l:front. "\<C-R>\""  . l:back
    endfunction
    vnoremap <C-l> :<C-u>call WrapSelect(input('Wrapping? Give both (space separated) or just the first one: '))<cr>
    

    To use, just highlight something, hit control l, and then type a character. If it's one of the characters the function knows about, it'll provide the correct terminating character. If it's not, it'll use the same character to insert on both sides.

    Surround.vim can do more than just this, but this was sufficient for my needs.

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

    surround.vim is going to be your easiest answer. If you are truly set against using it, here are some examples for what you can do. Not necessarily the most efficient, but that's why surround.vim was written.

    • Quote a word, using single quotes
      ciw'Ctrl+r"'
      • ciw - Delete the word the cursor is on, and end up in insert mode.
      • ' - add the first quote.
      • Ctrl+r" - Insert the contents of the " register, aka the last yank/delete.
      • ' - add the closing quote.

    • Unquote a word that's enclosed in single quotes
      di'hPl2x
      • di' - Delete the word enclosed by single quotes.
      • hP - Move the cursor left one place (on top of the opening quote) and put the just deleted text before the quote.
      • l - Move the cursor right one place (on top of the opening quote).
      • 2x - Delete the two quotes.

    • Change single quotes to double quotes
      va':s/\%V'\%V/"/g
      • va' - Visually select the quoted word and the quotes.
      • :s/ - Start a replacement.
      • \%V'\%V - Only match single quotes that are within the visually selected region.
      • /"/g - Replace them all with double quotes.
    0 讨论(0)
  • 2020-12-12 09:19

    VIM for vscode does it awsomely. It's based one vim-surround if you don't use vscode.

    Some examples:

    "test" with cursor inside quotes type cs"' to end up with 'test'

    "test" with cursor inside quotes type ds" to end up with test

    "test" with cursor inside quotes type cs"t and enter 123> to end up with <123>test

    test with cursor on word test type ysaw) to end up with (test)

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

    I'm using nnoremap in my .vimrc

    To single quote a word:

    nnoremap sq :silent! normal mpea'<Esc>bi'<Esc>`pl
    

    To remove quotes (works on double quotes as well):

    nnoremap qs :silent! normal mpeld bhd `ph<CR>
    

    Rule to remember: 'sq' = single quote.

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

    Here's some mapping that could help:

    :nnoremap <Leader>q" ciw""<Esc>P
    :nnoremap <Leader>q' ciw''<Esc>P
    :nnoremap <Leader>qd daW"=substitute(@@,"'\\\|\"","","g")<CR>P
    

    If you haven't changed the mapleader variable, then activate the mapping with \q" \q' or \qd. They add double quote around the word under the cursor, single quote around the word under the cursor, delete any quotes around the word under the cursor respectively.

    0 讨论(0)
提交回复
热议问题