Bash – How to set and determine the command-line editing mode of Bash

bashemacsvi

How to set the vi or emacs command line editing mode the Bash AND how to determine which mode is currently set?

Best Answer

Since your question is specific about bash:

To set it permanently for every new session:

echo 'set -o vi' >> ~/.bashrc

or (recommended), add (or change) a line in ./inputrc:

set editing-mode vi

This will set the editing mode of readline which is used by several other programs beside bash.

It is easy to unset both options:

shopt -ou vi emacs

To set one, either:

set -o vi

Or

shopt -os vi

The same for emacs. Setting vi unsets emacs and viceversa.

To list the state:

$ shopt -op emacs
set +o emacs

$ shopt -op vi
set -o vi

Or both at once:

$ shopt -op emacs vi
set +o emacs
set -o vi

To test if vi is set:

shopt -oq vi      &&   echo vi is set

Or (ksh syntax):

[[ -o vi ]]        &&   echo vi is set

emacs:

shopt -oq emacs   &&   echo emacs is set

Or:

[[ -o emacs ]]    &&   echo emacs is set

or, to test that no option is set:

! ( shopt -oq emacs || shopt -oq vi ) && echo no option is set
Related Question