如何在Vim中快速引用/取消引用单词并更改引用(例如从“到”)?我知道环绕的事。vim插件,但我想只使用vim。
当前回答
这个怎么样?
:%s/\'/"/g
其他回答
除了其他命令外,这将把一行中的所有单词用双引号括起来(根据您的注释)。
:s/\(\S\+\)/"\1"/
或者如果你想减少反斜杠的数量,你可以在模式的开头放一个\v(非常神奇的)修饰符
:s/\v(\S+)/"\1"/
环绕。Vim是最简单的答案。如果你真的反对使用它,这里有一些你可以做的例子。不一定是最有效的,但这就是为什么要环绕。Vim写完了。
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.
这个怎么样?
:%s/\'/"/g
如果你使用vim插件https://github.com/tpope/vim-surround(或使用VSCode vim插件,它附带了预先安装的vim-surround),它非常方便!
add
ysiw' // surround in word `'`
下降
ds' // drop surround `'`
改变
cs'" // change surround from `'` to `"`
它甚至适用于html标签!
cst<em> // change surround from current tag to `<em>`
查看github上的自述文件以获得更好的示例
我写了一个这样的脚本:
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>
要使用,只需要突出显示一些东西,按ctrl l,然后键入一个字符。如果它是函数所知道的字符之一,它将提供正确的结束字符。如果不是,它将在两边插入相同的字符。
环绕。Vim可以做的不仅仅是这些,但是这已经足够满足我的需求了。
推荐文章
- 在Vim/Vi中,如何将光标移动到前一个单词的末尾?
- 如何在vim中自动删除尾随空格
- 为什么Vim使用~扩展名保存文件?
- 如何在Vim或Linux中将空格转换为制表符?
- 我如何使用vimdiff来解决git合并冲突?
- 在Vim中删除当前缓冲区的文件名/路径
- Git在终端提交时打开VIM,但无法返回终端
- 请参阅编辑器中的换行符和回车
- 如何在NERDTree中显示隐藏文件(以句点开始)?
- 如何复制一个选择到OS X剪贴板
- 如何删除(不削减)在Vim?
- Vim:在可视模式下快速选择文本块的方法
- 是否有vim命令来重新定位一个选项卡?
- MacVim和普通Vim有什么区别?
- 有什么方法可以在Vim中查看当前映射的键吗?