我希望正常模式下的波浪符号
我发现我经常这样做,我想要一个仍然使用波浪符号的快捷方式。
~
,除了更改字母大小写外,还能够更改文本 ==
为!=
和!=
为 ==
。我发现我经常这样做,我想要一个仍然使用波浪符号的快捷方式。
~
,除了更改字母大小写外,还能够更改文本 ==
为!=
和!=
为 ==
。.vimrc
或从其他文件中source
此代码即可。" ----------------------
" Tilde switches ==/!=
" ----------------------
function! TildeSwitch()
" Gets the pair of characters under the cursor, before and behind.
let cur_pair = getline(".")[col(".") - 2 : col(".") - 1]
let next_pair = getline(".")[col(".") - 1 : col(".")]
if cur_pair == "=="
normal! "_ch!
normal! l
elseif next_pair == "=="
normal! r!
elseif cur_pair == "!="
normal! "_ch=
normal! l
elseif next_pair == "!="
normal! r=
else
" If == and != are not found, simply use the regular tilde.
normal! ~
endif
endfunction
nnoremap <silent> ~ :silent call TildeSwitch()<cr>
"_
在ch!
之前是什么意思?当进行更改时,寄存器不是自动填充的吗? - 244an_
)。我在那里使用该寄存器,以便“最后使用”的引号寄存器不会改变。http://vimdoc.sourceforge.net/htmldoc/change.html#quote_ - Juniper Belmont==
和!=
)之间进行切换只是切换多个选项的特例。我建议不要重载二进制~
命令,而是使用<C-A>
/<C-X>
。 SwapIt-可扩展关键字交换插件提供此功能,并实际上有一个默认选项可以切换==
,!=
,<=
等等。让我提出这个扩展~
命令的另一种实现方式:
nnoremap <silent> ~ :call SwitchNeq()<cr>~
function! SwitchNeq()
let [s, c] = [@/, getpos('.')]
s/[!=]\ze\%#=\|\%#[!=]\ze=/\='!='[submatch(0)=='!']/e
let @/ = s
call setpos('.', c)
endfunction
~
而不是 r!这真的让我很烦恼。我想在这里记录下我所做的事情,因为 Stack Overflow 对于像这样的小例子非常有用。 - Juniper Belmont