2

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?

7
  • 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, 2021 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, 2021 at 19:24
  • 1
    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, 2021 at 22:07
  • @mgarort set -o vi :)
    – D. Ben Knoble
    Aug 6, 2021 at 11:23
  • 1
    I had the same question (why I'm here): I wanted to have the same bindings in VI, terminal, and Mutt. it bothers me a bit that this doesn't seem to be a more popular question. makes me think there's something wrong with wanting to do this! Jan 21 at 1:32

1 Answer 1

3

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, 2021 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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