4

通常モードのコマンド tilde ~で、文字の大文字と小文字を変更するだけでなく、テキスト ==を to !=および !=to に変更できるようにしたいと考えてい==ます。

私はこれをかなり頻繁に行っていることがわかり、まだチルダを使用するショートカットが欲しい.

4

3 に答える 3

4

これは、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>
于 2013-02-04T17:03:01.173 に答える
2

~ この拡張コマンドの代替実装を提案させてください。

nnoremap <silent> ~ :call SwitchNeq()<cr>~

function! SwitchNeq()
    let [s, c] = [@/, getpos('.')]
    s/[!=]\ze\%#=\|\%#[!=]\ze=/\='!='[submatch(0)=='!']/e
    let @/ = s
    call setpos('.', c)
endfunction
于 2013-02-04T20:15:25.950 に答える
2

2 つの選択肢 (==と など!=) の間の切り替えは、複数のオプションの間の切り替えの特殊なケースにすぎません。バイナリ~コマンドをオーバーロードしないようにアドバイスし、代わりに<C-A>/を使用します<C-X>。SwapIt - 拡張可能なキーワード スワッパー プラグイン==はこれを提供し、実際には、!=<=などを切り替えるデフォルトのオプションがあります。

于 2013-02-05T07:12:00.827 に答える