Shell Command Line – What Is Meant by a Shell in ‘vi’ Mode or ’emacs’ Mode?

command linekeyboard shortcutsshellvi-modevim

This question follows directly from the answer. In this case I am specifically unable to understand the part which says:

In that regard, its behaviour is closer to emacs' than with
bash(readline)/ksh/zsh emacs mode, but departs from the terminal
driver embedded line editor (in canonical mode), where Ctrl-W deletes
the previous word (werase, also in vi).

Here we are talking about shells and not editors which are two completely different programs. What does it mean to say shell is in some editor mode?

P.S: You can base your answer on the premise that I understand what a shell is and how to use vim for basic editing.

Best Answer

In "vi" mode you can edit/navigate on the current shell prompt like a line in the vi editor. You can look at it like a one-line text file. Analogously in "emacs" mode you can edit/navigate the current command line using (some) of Emacs' shortcuts.

Example

For example in vi-mode you can do something like (in bash):

$ set -o vi
$ ls hello world
<ESC>
bbdw # results in
$ ls world

In emacs-mode you can hit e.g. Ctrl+A to jump at the start of a line (vi: Ctrl+[, 0 or ESC,0). You can turn on emacs mode via set -o emacs (in bash, ksh, zsh etc.).

Readline

A lot of interactive command line programs (including bash) use the readline library. Thus, you can configure which input mode to use (vi or emacs) and other options in one place such that every program using readline has the exact same editing/navigating interface.

For example my readline configuration looks like:

$ cat ~/.inputrc 
set editing-mode vi
set blink-matching-paren on

For example zsh/ksh does not use readline as far as I know, but also support vi/emacs modes that are very much like the bash/readline one.

Of course, the vi/emacs mode in a command line shell is just a subset of the complete editor feature set. Not every feature makes sense in a command line shell, and some features are more complicated to support than others.

Canonical Mode

Before vi/emacs modes of interactive command line shells 'were invented' your shell would use just the canonical mode of your terminal which only provides a limited set of editing commands (e.g. Ctrl+W to delete the last word.

Related Question