Adding navigation keybindings for vim command line
Asked Answered
A

2

6

Perhaps I'm not using the right terminology, but I'm struggling to try to find a way to add some key bindings for use while navigating the command line in vim.

An example would be the following command:

:e /really/long/path/that/I/dont/want/to/reenter

and realizing that I actually want to :tabe instead of tab, or entering a long regex pattern and discovering a typo earlier in it.

Obviously things like ^, 0 or b would just be entered as characters, so what I'd like to do is add a few emacs bindings for command mappings such as <C-a> to move to the beginning of the line, <C-e> to move to the end of the line and some others to move between words (at least those that don't conflict with other useful bindings).

Is this possible?

Angeliaangelic answered 18/6, 2012 at 20:50 Comment(1)
The default shortcuts for those actions are <C-B> and <C-E>. You can find more in the docs: vimdoc.sourceforge.net/htmldoc/usr_20.htmlThrong
O
14

As others have mentioned, your specific keybindings already exist:

  • Ctrl-b takes you to the beginning of the command-line, and
  • Ctrl-e takes you to the end of the command-line.

For the full command-line editing experience you can turn the vim command-line into an editable command buffer to address issues like this, rather than using new key bindings.

While typing in the command-line, hit Ctrl-f to enter the command-line buffer. You'll be in normal mode and can navigate around and edit your command line, as well as interact with and edit previous commands in your command history.

In your example, once in the command-line buffer, you could simply use 0itab to change e to tabe.

Hit Enter in this buffer to execute the command that your cursor is on, and Ctrl-c will exit the command-line buffer, dropping you back onto the command-line.

Odellodella answered 18/6, 2012 at 20:58 Comment(2)
+1 this will definitely be a more than daily used feature - I did not know until now! great!Computerize
Cool, I didn't think something like that existed. Will definitely use that.Angeliaangelic
C
6

here is how I would do it:

" adding emacs keybindings for command mode
cnoremap <c-a> <Home>
cnoremap <c-e> <End>

Edit:

In the help file :help cmdline I found

:cnoremap <C-A> <Home>
:cnoremap <C-F> <Right>
:cnoremap <C-B> <Left>
:cnoremap <Esc>b <S-Left>
:cnoremap <Esc>f <S-Right>

the last two are the ones for jumping around words - happy jumping

Computerize answered 18/6, 2012 at 20:54 Comment(2)
Do you know if there's a way to do this for word jumps as well?Angeliaangelic
Thank you thank you thank you. I always mess up my command line because my brain goes into command-line mode, which is readline/emacs keybindings.Boles

© 2022 - 2024 — McMap. All rights reserved.