通常モードのコマンド tilde ~
で、文字の大文字と小文字を変更するだけでなく、テキスト ==
を to !=
および !=
to に変更できるようにしたいと考えてい==
ます。
私はこれをかなり頻繁に行っていることがわかり、まだチルダを使用するショートカットが欲しい.
通常モードのコマンド tilde ~
で、文字の大文字と小文字を変更するだけでなく、テキスト ==
を to !=
および !=
to に変更できるようにしたいと考えてい==
ます。
私はこれをかなり頻繁に行っていることがわかり、まだチルダを使用するショートカットが欲しい.
これは、vimscript で行うのはかなり簡単です。.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>
~
この拡張コマンドの代替実装を提案させてください。
nnoremap <silent> ~ :call SwitchNeq()<cr>~
function! SwitchNeq()
let [s, c] = [@/, getpos('.')]
s/[!=]\ze\%#=\|\%#[!=]\ze=/\='!='[submatch(0)=='!']/e
let @/ = s
call setpos('.', c)
endfunction
2 つの選択肢 (==
と など!=
) の間の切り替えは、複数のオプションの間の切り替えの特殊なケースにすぎません。バイナリ~
コマンドをオーバーロードしないようにアドバイスし、代わりに<C-A>
/を使用します<C-X>
。SwapIt - 拡張可能なキーワード スワッパー プラグイン==
はこれを提供し、実際には、!=
、<=
などを切り替えるデフォルトのオプションがあります。