I would like my NeoVim to have Emacs-style keybindings in all "direct input" modes, in particular - Alt+d and Ctrl+k for forward word and line part deleting. I've done it for insert mode as

inoremap <M-d> <C-o>de
inoremap <C-k> <C-o>d$

But couldn't fine a way for colon-command mode - there is no Ctrl+o analog. Is it possible at all?

Also, is it possible to perform undo in colon-command mode? In bash(with default Emacs-style keybindings) one can perform undo with Ctrl+/. And in vim insert mode it could be mapped as inoremap <C-_> <C-o>u but I failed to find a way of mapping it in colon-command mode.

  • Your last question about undo is unclear (you don't explain what the problem with it is) and is fairly unrelated to the bulk of the question that talks about command-line mode. Please post that as a separate question and explain what doesn't work there, or what you want to achieve with it.
    – filbranden
    Dec 25 '20 at 15:11

There are a few ways you can handle command-line editing in Vim mappings.

For starters, there's CTRL-\ e which allow you to fully replace the contents of the command-line with the result of an expression. (The documentation at :help c_CTRL-\_e has example usage.)

In general, you can use getcmdline() to find what is currently in the command-line, in order to pick the parts that you want. There's also getcmdpos() and setcmdpos() to find where the cursor is or change its position.

An alternative to replacing the whole command-line is to emulate the action you want to implement by using repeated use of different keystrokes. For example, if you want a mapping to delete to the end of the current word, you could use getcmdline() and getcmdpos() to find how many characters are left in the word and then produce that number of "Delete" keystrokes, which you can easily do with repeat("\<Del>", n) and is well suited for a cnoremap <expr> kind of mapping.

If you care about command-line editing (and who among us doesn't?), rather than implementing a bunch of pinky-strain-inducing mappings for it, you might consider using the command-line window, which you can enter using q: from Normal mode, or CTRL-F from the command-line, where you can use Vim's other modes (Normal and Insert mode) to edit your command-line before having it executed. You also have access to the command-line history as lines in that window, which you can browse with k and j, or search with ?, etc.


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.