哪些 Vim 命令可用于引用/取消引用单词?
我如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
在单词周围添加引号: viw S '
将周围从 ' 更改为 ": cs ' "
Add quote to surrounding of word: v i w S '
Change surrounding from ' to ": c s ' "
我不知道任何内置 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 " andr'f"r'
to change from " to ' works if you stand on the first ' or ". The commandr'
replaces whatever character is under your cursor with ', andf"
moves you forward to the next ".添加引号
我开始在
.vimrc
中使用这个快速而肮脏的功能:所以现在,我直观地选择我想要的任何内容(通常通过
viw
-直观地选择引号中的内部单词),然后按Q表示双引号,或按q表示单引号。删除引号
我使用 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
: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
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 likef"vww
). Then I presss
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
.VIM for vscode 做得非常好。如果你不使用 vscode,它基于一个 vim-surround 。
VIM for vscode does it awsomely. It's based one vim-surround if you don't use vscode.
用引号将所有单词括起来:
之前:
之后:
wrap all words with quotes:
before:
after:
以下是一些可用于引用和取消引用单词的简单映射:
Here are some simple mappings that can be used to quote and unquote a word:
我使用的是
vim-surround
命令,本质上几乎是 vim。例如,要完全删除分隔符,请按
ds"
。更多详细信息请参见:
https://github.com/tpope/vim-surround
I am using the
vim-surround
command, almost vim in nature.for example, To remove the delimiters entirely, press
ds"
.More details are here:
https://github.com/tpope/vim-surround
我在 .vimrc 中使用 nnoremap
单引号一个单词:
删除引号(也适用于双引号):
要记住的规则: 'sq' = 单引号。
I'm using nnoremap in my .vimrc
To single quote a word:
To remove quotes (works on double quotes as well):
Rule to remember: 'sq' = single quote.
我编写了一个脚本来执行此操作:
要使用,只需突出显示某些内容,点击 control l,然后键入一个字符。如果它是函数知道的字符之一,它将提供正确的终止字符。如果不是,它将使用相同的字符在两侧插入。
Surround.vim 可以做的不仅仅是这个,但这足以满足我的需求。
I wrote a script that does this:
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.
在选定文本块周围添加单引号的可视模式映射示例:
Visual mode map example to add single quotes around a selected block of text:
在 NVIM 中,您可以使用此函数并映射
sw
或sW
来使用:lua Surround( "w")
和:lua Surround("W")
分别。在插入模式下按
sw
或sW
后,系统会询问您要包围的字符。如果你想用结束字符与开始字符不同的字符包围,只需输入开始字符即可。
注意,这在 VIM 中很容易复制。
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.
这个怎么样?
how about this?
您可以使用此行映射一个键以通过任何字符或字符对包围单词:
在本例中映射为“,您可以将其更改为您想要的任何内容。
如果您输入单个字符,它将用作包围符两个极端,如果您输入 2 个字符,它将在开头使用第一个字符,在末尾使用第二个字符:
您
You can map a key to surround a word by any char or pair of chars with this line:
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:
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.
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.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.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.这样对我来说更容易做到
It was easier for me to do it this way
如果您使用 vim 插件 https://github.com/tpope/vim-surround (或使用VSCode Vim插件,它预装了vim-surround),非常方便!
添加
删除
更改
它甚至适用于 html 标签!
查看 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
drop
change
It even works for html tags!
check out the readme on github for better examples
以下是一些可以提供帮助的映射:
如果您尚未更改 mapleader 变量,请使用
\q"
\q'
或\qd
Here's some mapping that could help:
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.宏方法!
按q和q记录到q寄存器(我们使用“q”作为记住“引号”的快捷方式)。
按shift + b将光标移动到当前单词前面
按 i 输入 ' (单引号)
按esc然后按e移动到末尾词
按a然后再次按'包围带引号的单词。
按esc进入正常模式。
最后按q将其记录到
q
寄存器中。如何使用
您可以用您喜欢的任何内容(一行、一个单词直到找到某个字符等)更改
第 4 步
。使录制的宏持久化
保存并退出
打开文件,转到一些单词
现在按
@q
如果你做得正确,神奇的东西就会出现在你的文字中。
您可以将其应用于您喜欢的其他宏。
The macro ways !
press q and q for recording into q register (we use "q" as shortcut to remember "quotes").
press shift + b move cursor to front of current word
press i type ' (a single quotes)
press esc then press e to move to end of word
press a then press ' again to surround the word with quotes.
press esc to get into normal mode.
finally press q to record it into
q
register.How to use
You can alter
step 4
with anything you like {a line, a word until found some character, etc}.Make recorded macro persistent
save and quit
open your files, go to some words
now press
@q
if you do it correctly, magic things should appear in your words.
You can apply this to other macros you loved.
除了其他命令之外,这还将用双引号将一行中的所有单词括起来(根据您的评论),
或者如果您想减少反斜杠的数量,您可以放置一个
\v
(非常-magic) 位于模式开头的修饰符In addition to the other commands, this will enclose all words in a line in double quotes (as per your comment)
or if you want to reduce the number of backslashes, you can put a
\v
(very-magic) modifier at the start of the pattern用单引号括起来(例如)
ciw'"'
可以,但 repeat 不起作用。尝试:这会将内容现在您可以在任何单词上按
.
将其括在引号中。要了解更多信息,请参阅: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: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/
对于 VSCodeVim、Neovim 和 Macvim 的用户,您可以执行
vwS"
"
替换为您想要换行的任何内容。w
不需要插件:)
For users of VSCodeVim, Neovim and Macvim, you can do
vwS"
"
with whatever you would like to wrap by.w
with any other selection operatorNo plugins needed :)