哪些 Vim 命令可用于引用/取消引用单词?

发布于 2024-08-19 12:23:58 字数 145 浏览 7 评论 0原文

我如何在 Vim 中快速引用/取消引用单词并更改引用(例如从 ' 更改为 ")? com/tpope/vim-surround" rel="noreferrer">surround.vim 插件,但我只想使用 Vim。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

静谧 2024-08-26 12:23:59

在单词周围添加引号: viw S '

  • viw: 选择光标下的单词
  • S: 添加周围
  • ': 单引号

将周围从 ' 更改为 ": cs ' "

  • cs : 更改周围
  • ': 单引号
  • ": 更改为双引号

Add quote to surrounding of word: v i w S '

  • viw: select word under the cursor
  • S: add surrounding
  • ': single quote

Change surrounding from ' to ": c s ' "

  • cs: change surrounding
  • ': single quote
  • ": change to double quote
居里长安 2024-08-26 12:23:59

我不知道任何内置 vim 命令,但使用 r"f'r" 从 ' 更改为 " 并使用 r'f"r' 更改“‘如果你站在第一个’或”,则有效。命令 r' 会将光标下的任何字符替换为 ',而 f" 将您移至下一个“。

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 ".

揪着可爱 2024-08-26 12:23:59

添加引号

我开始在 .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

所以现在,我直观地选择我想要的任何内容(通常通过 viw -直观地选择引号中的内部单词),然后按Q表示双引号,或按q表示单引号。

删除引号

vnoremap s <esc>:call StripWrap()<cr>

function! StripWrap()
  normal `>x`<x
endfunction

我使用 vim-textobj-quotes 这样 vim将引号视为文本对象。这意味着我可以执行 vaq (直观地选择引号周围。这会找到最近的引号并直观地选择它们。(这是可选的,您可以执行类似 的操作) f"vww)。然后我按s删除所选内容中的引号。

更改引号

KISS。我删除引号,然后添加引号
例如,要将单引号替换为双引号,我将执行以下步骤:
1. 删除单引号:vaqs,2. 添加新引号:vwQ


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.


还给你自由 2024-08-26 12:23:59

VIM for vscode 做得非常好。如果你不使用 vscode,它基于一个 vim-surround

一些例子:

“test”,光标在引号内,输入 cs"' 以“test”结尾

“test”,光标在引号内,输入 ds" 以 test 结尾

“test”,光标在引号内,输入 cs"t 并输入 123> 以结束
与<123>测试

将光标放在单词测试类型 ysaw) 上进行测试,最终得到 (test)

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)

酒浓于脸红 2024-08-26 12:23:59

用引号将所有单词括起来:

s/\(\w\+\)/"\1"/g

之前:

aaa,bbb,ccc

之后:

"aaa","bbb","ccc"

wrap all words with quotes:

s/\(\w\+\)/"\1"/g

before:

aaa,bbb,ccc

after:

"aaa","bbb","ccc"
通知家属抬走 2024-08-26 12:23:59

以下是一些可用于引用和取消引用单词的简单映射:

" '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>

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>
非要怀念 2024-08-26 12:23:59

我使用的是 vim-surround 命令,本质上几乎是 vim。

Surround.vim 都是关于“周围环境”的:圆括号、中括号、引号、XML 标记等等。该插件提供了映射,可以轻松地成对删除、更改和添加此类环境。

Plugin 'tpope/vim-surround' 

例如,要完全删除分隔符,请按 ds"
更多详细信息请参见:
https://github.com/tpope/vim-surround

I am using the vim-surround command, almost vim in nature.

Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.

Plugin 'tpope/vim-surround' 

for example, To remove the delimiters entirely, press ds".
More details are here:
https://github.com/tpope/vim-surround

笑梦风尘 2024-08-26 12:23:59

我在 .vimrc 中使用 nnoremap

单引号一个单词:

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

删除引号(也适用于双引号):

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

要记住的规则: 'sq' = 单引号。

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.

情话已封尘 2024-08-26 12:23:59

我编写了一个脚本来执行此操作:

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>

要使用,只需突出显示某些内容,点击 control l,然后键入一个字符。如果它是函数知道的字符之一,它将提供正确的终止字符。如果不是,它将使用相同的字符在两侧插入。

Surround.vim 可以做的不仅仅是这个,但这足以满足我的需求。

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.

も星光 2024-08-26 12:23:59

在选定文本块周围添加单引号的可视模式映射示例:

:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>

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

:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
翻了热茶 2024-08-26 12:23:59

NVIM 中,您可以使用此函数并映射 swsW 来使用 :lua Surround( "w"):lua Surround("W") 分别。

在插入模式下按 swsW 后,系统会询问您要包围的字符。

如果你想用结束字符与开始字符不同的字符包围,只需输入开始字符即可。

注意,这在 VIM 中很容易复制。

function Surround(w_or_W)
    local open_char = vim.fn.input("Surround with: ")
    local closed_char = nil
    if open_char == "(" then closed_char = ")" end
    if open_char == "[" then closed_char = "]" end
    if open_char == "{" then closed_char = "}" end
    if open_char == "<" then closed_char = ">" end
    if open_char == "'" then closed_char = "'" end
    if open_char == '"' then closed_char = '"' end
    if open_char == "`" then closed_char = "`" end
    if open_char == "/" then closed_char = "/" end
    if open_char == "|" then closed_char = "|" end

    if w_or_W == "w" then
        vim.cmd("normal! ciw" .. open_char)
    elseif w_or_W == "W" then
        vim.cmd([[normal! ciW]] .. open_char)
    end
    vim.cmd("normal! p")
    vim.cmd("normal! a" .. closed_char)
    vim.cmd("normal! a")
end

vim.api.nvim_set_keymap("n", "<leader>sw", ":lua Surround('w')<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<leader>sW", ":lua Surround('W')<CR>", { noremap = true, silent = true })

In NVIM you can use this function and map <leader>sw or <leader>sW to use :lua Surround("w") and :lua Surround("W") respectively.

After pressing <leader>sw or <leader>sW in insert mode you will be asked for the character you want to surround with.

If you want to surround with characters whose closing character is different from the opening character, just type the opening one

Note that this is easily reproduced in VIM.

function Surround(w_or_W)
    local open_char = vim.fn.input("Surround with: ")
    local closed_char = nil
    if open_char == "(" then closed_char = ")" end
    if open_char == "[" then closed_char = "]" end
    if open_char == "{" then closed_char = "}" end
    if open_char == "<" then closed_char = ">" end
    if open_char == "'" then closed_char = "'" end
    if open_char == '"' then closed_char = '"' end
    if open_char == "`" then closed_char = "`" end
    if open_char == "/" then closed_char = "/" end
    if open_char == "|" then closed_char = "|" end

    if w_or_W == "w" then
        vim.cmd("normal! ciw" .. open_char)
    elseif w_or_W == "W" then
        vim.cmd([[normal! ciW]] .. open_char)
    end
    vim.cmd("normal! p")
    vim.cmd("normal! a" .. closed_char)
    vim.cmd("normal! a")
end

vim.api.nvim_set_keymap("n", "<leader>sw", ":lua Surround('w')<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<leader>sW", ":lua Surround('W')<CR>", { noremap = true, silent = true })
无人接听 2024-08-26 12:23:59

这个怎么样?

 :%s/\'/"/g

how about this?

 :%s/\'/"/g
暮年慕年 2024-08-26 12:23:59

您可以使用此行映射一个键以通过任何字符或字符对包围单词:

noremap " yiw :let surrounder=input('surrounder(s)? ') <bar> :execute 'normal ciw'.surrounder[0].'<c-r>"'.(surrounder[1]??surrounder[0]).'<c-v><esc>'<cr> 

在本例中映射为“,您可以将其更改为您想要的任何内容。

如果您输入单个字符,它将用作包围符两个极端,如果您输入 2 个字符,它将在开头使用第一个字符,在末尾使用第二个字符:

  1. 在单词上按“并输入 ' 将导致“单词”,
  2. 在单词上按“并输入 []。将导致[词]

You can map a key to surround a word by any char or pair of chars with this line:

noremap " yiw :let surrounder=input('surrounder(s)? ') <bar> :execute 'normal ciw'.surrounder[0].'<c-r>"'.(surrounder[1]??surrounder[0]).'<c-v><esc>'<cr> 

in this case is mapped to ", you can change it to whatever you want.

If you enter a single char it will be used as surrounder in both extremes, if you enter 2 chars it will use the first at the beginning and the second at the end.

Ejs:

  1. you press " over a word and enter ' will result in 'word'
  2. you press " over a word and enter [] will result in [word]
听你说爱我 2024-08-26 12:23:58

Surround.vim 将是您最简单的答案。如果您确实反对使用它,这里有一些您可以采取的措施的示例。不一定是最有效的,但这就是编写 around.vim 的原因。

  • 使用单引号引用一个单词
    ciw'Ctrl+r"'

    • ciw - 删除光标所在的单词,并最终进入插入模式。
    • ' - 添加第一个引号。
    • Ctrl+r" - 插入 " 寄存器的内容,也称为最后一次复制/删除。
    • ' - 添加结束引号。
  • 取消引用单引号内的单词
    di'hPl2x

    • di' - 删除单引号括起来的单词。
    • hP - 将光标向左移动一个位置(在开头引用的顶部)并将刚刚删除的文本放在引用之前。
    • l - 将光标向右移动一个位置(在起始引号的顶部)。
    • 2x - 删除两个引号。
  • 将单引号改为双引号
    va':s/\%V'\%V/"/g

    • va' - 直观地选择引用的单词和引号。
    • :s/ - 开始替换。
    • \%V'\%V - 仅匹配可视选择区域内的单引号。
    • /"/g - 将它们全部替换为双引号。

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.
丘比特射中我 2024-08-26 12:23:58

引用一个单词,使用单引号

ciw'Ctrl+r"'

这样对我来说更容易做到

ciw '' Esc P

Quote a word, using single quotes

ciw'Ctrl+r"'

It was easier for me to do it this way

ciw '' Esc P
半世晨晓 2024-08-26 12:23:58

如果您使用 vim 插件 https://github.com/tpope/vim-surround (或使用VSCode Vim插件,它预装了vim-surround),非常方便!

添加

ysiw' // surround in word `'`

删除

ds' // drop surround `'`

更改

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

它甚至适用于 html 标签!

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

查看 github 上的自述文件以获得更好的示例

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

拥抱我好吗 2024-08-26 12:23:58

以下是一些可以提供帮助的映射:

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

如果您尚未更改 mapleader 变量,请使用 \q" \q'\qd

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.

避讳 2024-08-26 12:23:58

宏方法!

  1. qq记录到q寄存器(我们使用“q”作为记住“引号”的快捷方式)。

  2. shift + b将光标移动到当前单词前面

  3. i 输入 ' (单引号)

  4. esc然后按e移动到末尾词

  5. a然后再次按'包围带引号的单词。

  6. esc进入正常模式。

  7. 最后按q将其记录到q寄存器中。

如何使用

  1. 将光标移动到所需的单词。
  2. @q 用引号将单词括起来。
  3. 如果您想将其重复到另一个单词中,请按@@

您可以用您喜欢的任何内容(一行、一个单词直到找到某个字符等)更改第 4 步

使录制的宏持久化

  1. 打开 .vimrc
  2. 转到文件末尾
  3. 更改为插入模式。输入以下内容以使其持久化:
let @q='ctrl + r ctrl + r q'
  1. 保存并退出

  2. 打开文件,转到一些单词

  3. 现在按@q

如果你做得正确,神奇的东西就会出现在你的文字中。

您可以将其应用于您喜欢的其他宏。

The macro ways !

  1. press q and q for recording into q register (we use "q" as 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 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.

帥小哥 2024-08-26 12:23:58

除了其他命令之外,这还将用双引号将一行中的所有单词括起来(根据您的评论),

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

或者如果您想减少反斜杠的数量,您可以放置​​一个 \v (非常-magic) 位于模式开头的修饰符

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

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"/
凉薄对峙 2024-08-26 12:23:58

用单引号括起来(例如) ciw'"' 可以,但 repeat 不起作用。尝试:

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

这会将内容现在您可以在任何单词上按 . 将其括在引号中。要了解更多信息,请参阅 :h[elp] i_ctrl-r 以及有关文本对象的更多信息,请访问 :h[elp] i_ctrl-r 以及有关文本对象的更多信息。 appspot.com/motion.txt.html#text-objects" rel="noreferrer">:h text-objects

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

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/

假装不在乎 2024-08-26 12:23:58

对于 VSCodeVim、Neovim 和 Macvim 的用户,您可以执行

vwS"

  • 您可以将 " 替换为您想要换行的任何内容。
  • 您可以用任何其他选择运算符替换 w

不需要插件:)

For users of VSCodeVim, Neovim and Macvim, you can do

vwS"

  • You can replace " with whatever you would like to wrap by.
  • You can replace w with any other selection operator

No plugins needed :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文