9

I noticed when in vi mode in Bash (i.e., the mode enabled with "set -o vi"), that some commands, such as "diw", that work in Vim, but not in vi, don't work on the Bash command line. Is there an easy way to configure Bash so that its keybindings will support Vim commands?

I would like to be able to enter Vim commands on the command line without having to actually start the Vim program, as described in this question.

2
  • 7
    bash doesn't support vi or vim commands; rather, it has keybindings similar to vi's bindings for its own command set. Commented Sep 11, 2017 at 18:13
  • I've edited the question. Commented Sep 12, 2017 at 19:35

4 Answers 4

14

First, the "vi mode" you get with set -o vi is not vi itself. It's an incomplete approximation of vi's behavior built into readline, the command-line editing library used by bash under the hood.

Second, because it is incomplete there's no reason whatsoever to expect every vi command to work.

Third, no, there's no "vim mode" so even less reason to expect any vim commands to work.

Fourth, if you absolutely want to edit the current command-line with Vim-like commands, why don't you go all the way and… actually use Vim:

<C-x><C-e>

That said, $ man readline tells you everything you need to customize its behavior and add bindings.

Sign up to request clarification or add additional context in comments.

3 Comments

If you're already in vi mode, then the equivalent of <C-x><C-e> is ESC+v.
See this to make ctrl-r work to clear screen unix.stackexchange.com/questions/104094/…
In case it helps anyone - look for the "VI Mode bindings" section at the very end of the man readlineoutput.
7

The best way of doing this that I know of would be to use Athame. It can be a surprisingly powerful experience in some cases. I particularly like it for interacting with a REPL.

Athame patches your shell to add full Vim support by routing your keystrokes through an actual Vim process. Athame can currently be used to patch readline (used by bash, gdb, python, etc) and/or zsh (which doesn't use readline).

Enter image description here

Alternatively, I find Spacemacs with the eshell to be a reasonably functional, if strange, solution.

3 Comments

If this is the kind of answer the question is looking for, then that question is off-topic (from stackoverflow.com/help/on-topic, in the "some questions are still off-topic" list, note the "recommend or find a book, tool, software library, or other off-site resource" entry).
@CharlesDuffy I don't believe that makes it of topic. Here is looking for a solution to his problem. One potential solution is this tool. He is not specifically looking for tools.
Granted. The more compelling reason the question is off-topic is that it isn't about writing code, developing scripts, or otherwise unique to software development (as specified behind the above link), but is instead about interactive shell configuration and use -- a topic for Unix & Linux SE or SuperUser.
3

Teach vi-command-mode diw to any software that uses readline (such as Bash) by adding this to your ~/.inputrc file:

set keymap vi-command
"diw": "bde"

3 Comments

Would you please educate me? Short search didn't gave meaning to this. Thanks
Read tiswww.case.edu/php/chet/readline/… under the heading “Key Bindings”. TL;DR: that config line tells readline to interpret the key sequence “diw” as though you had typed “bde“ (go to start of word, delete to end of word)
Thanks a lot Luke. Have a nice one :)
2

You can use ble.sh. Which is a command line editor written in pure Bash which replaces the default GNU Readline. I like to call it "zsh for bash".

Appart from vim-style navigation it gives you:

After installation don't forget to add (recommended) to your .bashrc:

# at the start of your .bashrc file
[[ $- == *i* ]] && source /usr/share/blesh/ble.sh --noattach

...

#for vim-style mode
set -o vi

...

#at the end of your .bashrc file
[[ ${BLE_VERSION-} ]] && ble-attach

or you can just:

# at the start of your .bashrc file
source /usr/share/blesh/ble.sh

but this may not work as expected - read this.

2 Comments

Thanks, it is what I set but I don't know how to get rid of --INSERT--. Anyone has a idea how to do it?
Oh never mind, I found the solution is to create .blerc and put in something from here

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.