如何在Vim中快速引用/取消引用单词并更改引用(例如从“到”)?我知道环绕的事。vim插件,但我想只使用vim。
当前回答
在选定的文本块周围添加单引号的可视化模式映射示例:
:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
其他回答
我写了一个这样的脚本:
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命令,但使用r"f'r"从'到'和r'f"r'从'到'工作,如果你站在第一个'或'。命令r'将光标下的任何字符替换为',f“将您向前移动到下一个”。
如果你使用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上的自述文件以获得更好的示例
用单引号括起来(例如)ciw'<C-r>"'<esc>可以,但重复就不行。试一试:
ciw'<C-r><C-o>"'<esc>
这将默认寄存器的内容“逐字”放置。现在你可以按了。任何一个词都要加引号。要了解更多信息,请参阅:h[elp] i_ctrl-r和关于:h text-objects文本对象的更多信息
来源:http://vimcasts.org/episodes/pasting-from-insert-mode/
我在我的。vimrc中使用nnoremap
单引用一个词:
nnoremap sq :silent! normal mpea'<Esc>bi'<Esc>`pl
删除双引号(也适用于双引号):
nnoremap qs :silent! normal mpeld bhd `ph<CR>
记住规则:'sq' =单引号。
推荐文章
- 如何在Vim注释掉一个Python代码块
- 如何在可视块模式插入?
- 强迫自己掌握vi的最好方法是什么?
- 如何回到行之前编辑的最后一个在Vim?
- 如何跳转到一个特定的人物在vim?
- 如何删除一个大的文本块而不计算行?
- 在Vim/Vi中,如何将光标移动到前一个单词的末尾?
- 如何在vim中自动删除尾随空格
- 为什么Vim使用~扩展名保存文件?
- 如何在Vim或Linux中将空格转换为制表符?
- 我如何使用vimdiff来解决git合并冲突?
- 在Vim中删除当前缓冲区的文件名/路径
- Git在终端提交时打开VIM,但无法返回终端
- 请参阅编辑器中的换行符和回车
- 如何在NERDTree中显示隐藏文件(以句点开始)?