Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to switch Bash or zsh from Emacs mode to vi mode with a keystroke?

Tags:

bash

zsh

history

I'd like to be able to switch temporarily from emacs mode to vi mode, since vi mode is sometimes better, but I'm usually half-way through typing something before I realize I want to use vi mode.

I don't want to switch permanently to vi mode, because I normally prefer emacs mode on the command line, mostly because it's what I'm used to, and over the years many of the keystrokes have become second nature. (As an editor I generally use emacs in viper mode, so that I can use both vi and emacs keystrokes, since I found myself accidentally using them in vi all the time, and screwing things up, and because in some cases I find vi keystrokes more memorable and handy, and in other cases emacs.)

like image 324
iconoclast Avatar asked Apr 14 '10 18:04

iconoclast


People also ask

How do I set bash to vi mode?

The bash shell (again, via GNU Readline) is able to provide this functionality for us. In order to enable it, you run the command $ set -o vi.

Which of the following is the default command line editing mode in bash vi Ed Emacs Vim?

Bash provides two modes for command line editing - emacs and vi. Emacs editing mode is the default and I already wrote an article and created a cheat sheet for this mode.


1 Answers

You can create a toggle since the key bindings are separate between vi mode and emacs mode.

$ set -o emacs
$ bind '"\ee": vi-editing-mode'
$ set -o vi
$ bind '"\ee": emacs-editing-mode'

Now Alt-e (or Esc e) will toggle between modes.

Add this somewhere in your definition for PS1 so you have an indicator in your prompt of which mode you're in. It won't show the change immediately when you toggle modes, but it will update when a new prompt is issued.

$(set -o | grep emacs.*on >/dev/null 2>&1 && echo E || echo V)
like image 136
Dennis Williamson Avatar answered Nov 16 '22 03:11

Dennis Williamson