1

I am trying to emulate readline keybindings in insert and command-line modes (for example, <C-u> for deleting from cursor position to beginning of the line, or <C-w> to delete previous word). The readline action kill-line deletes from the current cursor position to the end of the line, and it is usually bound to <C-k>. I have managed to emulate it in insert mode with

inoremap <Plug>(i_kill-line) <Esc>lC
imap <C-k> <Plug>(i_kill-line)

How could I do something similar in command-line mode?

5
  • 1
    I understand you may still want to do things as you describe but are you familiar with the command-line window? It's a great way to do command-line editing. You can use the full power of Vim rather than a smattering of Vim-like keystrokes.
    – B Layer
    Aug 5 at 19:12
  • C-u and C-w should already work insert mode. At any rate for command-line mode you need cnoremap, and as BLayer says the command-line window makes editing even nicer.
    – D. Ben Knoble
    Aug 5 at 19:24
  • Thank you for your answers! I am aware of the command-line window and I should probably use it more. However, I like to have the same basic keybindings in the main places where I can type (mainly Bash, Vim and fzf window) because that makes it easier to learn them and use them mechanically. <C-k> was recently added to my vocabulary, so I wanted to make it available in Vim.
    – mgarort
    Aug 5 at 22:07
  • @mgarort set -o vi :)
    – D. Ben Knoble
    Aug 6 at 11:23
  • Indeed :) But I find simple emacs keybindings more appropriate for the command line, and also I have personalized Vim in a way that set -o vi cannot replicate. So I would have a similar problem of non-homogeneity with set -o vi.
    – mgarort
    Aug 6 at 23:25
2

Use combination of :h c_CTRL-\_e, :h getcmdline() and :h getcmdpos():

func! KillLineToEnd()
    return strpart(getcmdline(), 0, getcmdpos()-1)
endfunc
cnoremap <C-k> <C-\>eKillLineToEnd()<CR>
1
  • Very impressive. Thank you!
    – mgarort
    Aug 6 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.