4

我希望普通模式命令 tilde ~除了更改字母的大小写之外,还能够将文本 更改 ==为 !=和 。!===

我发现我经常这样做,我想要一个仍然使用波浪号的快捷方式。

4

3 回答 3

4

这在 vimscript 中相当简单。从不同的文件将以下内容添加到您的.vimrcsource此代码中。

" ----------------------
"  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

在两个选项(如==!=)之间切换只是在多个选项之间切换的一种特殊情况。我建议不要重载二进制~命令,而是使用<C-A>/ <C-X>。SwapIt - 可扩展的关键字交换插件提供了这一点,并且实际上有一个默认选项来切换==, !=,<=等。

于 2013-02-05T07:12:00.827 回答