Vim key binding that mimics emacs CTL-K

I am trying to create a vim keybind that mimics emacs CTL-K:

  • If used at the end of a line, it kills the newline ending at the end of the line by merging the next line into the current one (thus, the empty line is completely removed).
  • Otherwise, Ck kills all the text from point to the end of the line;
  • If point was originally at the beginning of a line, this leaves the line empty.

I saw the answer at https://unix.stackexchange.com/a/301584/137686 recommending the following

inoremap <C-K> <Esc>lDa

      

This seems to work for case 2, but not for case 1 (it won't remove the newline character) or 3 (it will leave the first character in the line). Any recommendation on how I can improve the display to achieve all three?

+3


source to share


3 answers


Give this mapping a expr

try:

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

      

It checks the current cursor position to select D

or J

.



c-o

provides a return to insert mode after surgery.

Note

Insert mode is ctrl-k

very useful for entering digraphs. Think twice if you want to disable the feature with your mapping.

+7


source


I wrote a function for this:



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

      

+4


source


If you need a one-line display for playback, you can use this:

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

I haven't tested for edge cases, but I'm sure this will be enough to get you started.

+1


source







All Articles