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

2020-02-02 03:35发布

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 just Vim.

标签: vim quoting
15条回答
放我归山
2楼-- · 2020-02-02 04:13

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.

查看更多
Melony?
3楼-- · 2020-02-02 04:13

how about this?

 :%s/\'/"/g
查看更多
对你真心纯属浪费
4楼-- · 2020-02-02 04:15

Quote a word, using single quotes

ciw'Ctrl+r"'

It was easier for me to do it this way

ciw '' Esc P
查看更多
狗以群分
5楼-- · 2020-02-02 04:21

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

:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
查看更多
\"骚年 ilove
6楼-- · 2020-02-02 04:23

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
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-02-02 04:24

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.
查看更多
登录 后发表回答