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?
source to share
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.
source to share