Vim 模仿 emacs CTL-K 的键绑定

Vim key binding that mimic emacs CTL-K

我正在尝试创建一个模仿 emacs CTL-K 的 vim 键绑定:

  1. If used at the end of a line, it kills the line-ending newline character, merging the next line into the current one (thus, a blank line is entirely removed).
  2. Otherwise, C-k kills all the text from point up to the end of the line;
  3. if point was originally at the beginning of the line, this leaves the line blank.

我在 https://unix.stackexchange.com/a/301584/137686 看到了一个回答,推荐以下内容

inoremap <C-K> <Esc>lDa

它似乎适用于情况 2,但不适用于情况 1(它不会删除换行符)或情况 3(它将保留行中的第一个字符)。关于如何改进映射以实现所有这三个目标,有什么建议吗?

我写了一个函数来实现这个:

inoremap <C-K> <c-r>=CtrlK()<cr>

function! CtrlK()
    let col = col('.')
    let end = col('$')

    if col == end
        return "\<Del>"
    elseif col == 1
        return "\<Esc>cc"
    else
        return "\<Esc>lc$"
    endif
endfunction

如果你需要一个单线映射来玩,你可以使用这个:

inoremap <C-K> <Esc>:if col(".")==col("$")-1\|exe "normal gJh"\|else\|exe "normal lD"\|endif<Enter>a

我还没有针对边缘情况进行测试,但我相信这足以让您入门。

试试这个 expr 映射:

inoremap <expr> <c-k> '<c-o>'.(col('.')==col('$')?'J':'D')

它会检查您当前的光标位置,以决定执行 DJ

c-o确保return运行后进入插入模式

备注

插入模式ctrl-k对于输入二合字母非常有用。如果您想使用映射禁用该功能,请三思。