如何在Vim中快速引用/取消引用单词并更改引用(例如从“到”)?我知道环绕的事。vim插件,但我想只使用vim。
当前回答
引用一个词,使用单引号 被'Ctrl + r”
这样做对我来说比较容易
ciw '' Esc P
其他回答
在单词的周围加上引号:v i w S '
视图:选择光标下的单词 S:添加周边 ':单引号
将'改为":c s ' "
c:改变环境 ':单引号 :改为双引号
我使用的是vim-surround命令,本质上几乎是vim。
环绕。vim都是关于“环境”的:圆括号、括号、引号、XML标记等等。该插件提供了映射,可以轻松地成对删除、更改和添加这样的环境。
Plugin 'tpope/vim-surround'
例如,要完全删除分隔符,请按ds”。 详情如下: https://github.com/tpope/vim-surround
用引号括住所有单词:
s/\(\w\+\)/"\1"/g
之前:
aaa,bbb,ccc
后:
"aaa","bbb","ccc"
我写了一个这样的脚本:
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可以做的不仅仅是这些,但是这已经足够满足我的需求了。
这里有一些简单的映射,可以用来引用和取消引用一个单词:
" '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>