20

When I'm editing a Vim command, I would like to use the same shortcuts as in Bash and every other REPL: M-b to go back a word, M-Backspace to delete a previous word, M-u to convert the word to uppercase, C-k to cut until the end of the line, etc. I have been able to configure some of the commands, using :cmap, but not all.

Is there a plugin or setting which provides this?

I know about cedit, but I find it cumbersome when all I need is to enter a quick command.

3
  • Waitaminute, are you saying you use Bash in Emacs mode even though you use Vim as your editor? Jan 5, 2016 at 20:14
  • 4
    @KyleStrand I've yet to see someone who used Bash in vi mode. It seems like most of the benefits of modal editors disappear when you're only limited to one line.
    – Mihai
    Jan 5, 2016 at 20:41
  • 2
    Interesting. I only ever use Bash in Vi mode, though it does seem like Emacs mode supports some things that Vi mode doesn't. I just happen to know Vi keybindings while remaining almost completely ignorant of Emacs bindings, and I've never thought it a pressing enough issue to learn the defaults. Jan 5, 2016 at 20:46

4 Answers 4

15

Is there a plugin or setting which provides this?

Yes, rsi.vim plugin:

Features

  • Readline mappings are provided in insert mode and command line mode. Normal mode is deliberately omitted.
  • Important Vim key bindings (like insert mode's C-n and C-p completion) are not overridden.
  • Meta key bindings are provided in a way that works in the terminal without the perils of remapping escape.
  • C-d, C-e, and C-f are mapped such that they perform the Readline behavior in the middle of the line and the Vim behavior at the end. (Think about it.)
6

Is there a plugin or setting which provides this?

There is also readline.vim.

It is a newer plug-in which focuses purely on the command-line. It also implements a larger subset of the Readline shortcuts and has the goal of implementing each shortcut exactly like in Readline.

What makes this plugin different from similar plugins is that it implements a larger subset of the Readline mappings, and that it does a better job of mimicking the Readline behavior for each command.

The word movement and deletion commands have different behavior between Vim and Readline. The biggest difference is that in Readline punctuation is always skipped when searching for a word boundary. Another difference is that _ (underscore) is treated as a word delimiter. This plugin implements the Readline behavior for word movement and deletion commands.

4
  • 1
    I'm surprised there's anyone looking for this kind of thing. It's so easy to use readline's vim mode that I'd think every regular vim user would do it. (I don't find the extra commands in emacs mode all that compelling.) But maybe the "how" is not that widely known (...and maybe I'm lucky that the very first bashrc I saw/copied...many more years ago than I care to admit...had set -o vim :)
    – B Layer
    May 11, 2019 at 13:41
  • @BLayer No it's not about people not knowing Readline's Vi mode. It's about the fact that text editing and CLI sessions are two very different things and the first one is far more suited for modal editing than the second one, as can be very clearly observed from this post since everyone looking for this here obviously knows about Vim keybindings and the fact that you can edit vim command mode via Vim keybindings too with Ctrl+F. People are specifically looking for this because CLI editing is better approached with modeless editing. You should stop projecting your experience onto others.
    – adamency
    Mar 20 at 17:51
  • @BLayer The wide majority of experienced vim users I've known still used default emacs keybindings in their shell sessions. By the way, I believe tpope, the author of vim-rsi knows a thing or two about Readline's Vim mode...
    – adamency
    Mar 20 at 17:56
  • @adamency What you should do is learn to read. My comment is not in any way negative or casting aspersions on anyone. It is simply an expression of my own mild surprise. Never heard of you before but now that I have I think you're kinda creepy. Cheers!
    – B Layer
    Mar 21 at 6:53
3

I’ll add my most common alternatives:

  • Use the default :-editing bindings (:help commandline-editing or something similar, if anyone has the reference offhand feel free to edit). <C-b> is beginning, <C-e> is end, and there are others
  • Use the wonderful <C-f> if you started a :, ?, or / and want to edit like it’s a normal vim buffer.
  • Use q:, q?, or q/ if you know you need this when you start.

I have <leader>; mapped to q: so a vim-edited command-line is very quick access. (I prefer not to switch ; and : in my everyday editing, but I’m considering it.)

1

For Neovim users, there is also https://github.com/linty-org/readline.nvim.

Disclaimer: I'm the author.

Your Answer

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

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