Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding navigation keybindings for vim command line

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?

like image 548
Peter Zich Avatar asked Jun 18 '12 20:06

Peter Zich


2 Answers

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.

like image 164
pb2q Avatar answered Oct 21 '22 04:10

pb2q


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

like image 43
epsilonhalbe Avatar answered Oct 21 '22 04:10

epsilonhalbe