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.

  • This is not about core shell functionality per se, but rather about line editing (what you do when you make a typo and go back and correct yourself). Commented Aug 3, 2013 at 7:05

2 Answers 2


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.


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

$ set -o vi
$ ls hello world
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.).


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.

  • Suppose I don't know on which Input mode I am on. Can I verify it by entering some text and press [Ctrl] + [A]? if cursor moves to beginning its emacs else it vi?
    – limovala
    Commented Aug 6, 2013 at 6:30
  • 2
    @limovala, should be a good approximation. Depends on your shell of course - if CTL+A does not work another possibility is that your shell does not include any editing mode. Perhaps some shells also implement other editing modes. But in practice your method should be good enough. You can also test with a vi command after that to be more sure. In bash you can also use something like set -o | grep 'emacs\|vi'. In zsh (where I have vi-mode) this does not work, though. Commented Aug 6, 2013 at 7:35
  • bind -P will also give good indication of which mode one is in
    – Paul
    Commented Dec 8, 2015 at 13:00

You'll notice that when you run cat at a shell prompt on a terminal, cat being supposed to write to stdout what it reads from stdin, and press a, you see a a echoed back by the terminal driver, but cat doesn't write that a (you see only one a, the one echoed by the terminal driver).

However, if you type a Backspace b Enter, you don't see cat outputting a\010b\015, but b\012 (b and newline).

That is because the terminal driver (we're talking software in the kernel, not in the terminal emulator like xterm) implements a very basic line editor when in canonical mode. The terminal driver can be configured using ioctl() system calls like when using the stty command. For instance, to leave the canonical mode, you can do stty -icanon. If you do:

stty -icanon; cat

Then, you'll see both the echo (which you could have disabled with stty -echo) and the cat output at the same time.

That editor is a line editor. That is, it is for the user to edit one line of text until it's sent to the application reading the terminal device upon pressing Enter.

The editing capabilities of that editor are very limited. In most implementations, there are only 4 editing keys (actually characters) also configurable with stty:

  • erase (^H or ^? usually): erase the previous character
  • kill (^U usually): empty (kill) the line entered so far
  • werase (^W): erase the previous word
  • lnext (^V): enter the next character literally (cancel the special meaning of all of the above)

Back in the old days, it was thought that that terminal driver line editor would be extended with fancier capabilities. That is why none of the early shells has any command line editing capabilities (you'd get the same line editing capabilities at the shell prompt than when running cat like we did above).

However, that really never happened, maybe part of the reason being the mess with different terminals not sending the same characters upon some key presses which made it evident that that should not be implemented in kernel space.

So some shells started dropping the canonical mode of the terminal driver and implementing their own line editor. At the time, emacs and vi were the most popular visual text editors with completely different key binding and operation mode. In vi, you have one mode for entering text, and one for editing. In emacs, you're always in entering text mode, but editing is done by pressing key combinations (like ^b to move the character backward).

There was no point for shells at the time to come up with their own different key binding. That would have caused frustration for people to have to learn a different one. However, choosing one (emacs or vi) style over the other would have been a sure way to alienate the users of the other editor.

According to https://www.usenix.org/legacy/publications/library/proceedings/vhll/full_papers/korn.ksh.a:

The popular inline editing features (vi and emacs mode) of ksh were created by software developers at Bell Laboratories; the vi line editing mode by Pat Sullivan, and the emacs line editing mode by Mike Veach. Each had independently modified the Bourne shell to add these features, and both were in organizations that wanted to use ksh only if ksh had their respective inline editor. Originally the idea of adding command line editing to ksh was rejected in the hope that line editing would move into the terminal driver. However, when it became clear that this was not likely to happen soon, both line editing modes were integrated into ksh and made optional so that they could be disabled on systems that provided editing as part of the terminal interface.

So instead, they implemented both and an interface for users to choose between the two. ksh was most probably the first in the early 80s (reusing code that had been written separately to add a vi mode and an emacs mode to the Bourne shell as seen above) followed by tcsh (tcsh initially only had emacs key binding, vi mode was added later) and later bash and zsh in the early 90s.

You switch between the two modes in bash, zsh or ksh with set -o vi or set -o emacs, and with bindkey -e or bindkey -v in tcsh or zsh.

POSIX actually specifies the vi mode and not the emacs mode for sh (the story has that Richard Stallman objected to POSIX specifying the emacs mode for sh).

The default mode for bash, the public domain variants of ksh (pdksh, mksh, oksh), tcsh and zsh is the emacs mode (though with zsh, it's vi if your $EDITOR is vi), while in the AT&T ksh, it's the dumb mode unless $EDITOR or $VISUAL mentions vi or emacs.

ksh also later added a gmacs mode to accommodate users of Gosling emacs that handled Ctrl+T differently.

Now the handling of ^W in emacs or in tcsh emacs mode probably predates the werase character in the terminal line editor, so we can't really blame them for that and my statement about "departing..." may be seen as misleading. It's just that I find it irritating when things like emacs, tcsh or info behave differently from everything else when you type Ctrl-W. You can imagine I found it a lot more irritating when some applications started to close their window when you typed Ctrl-W.

  • 1
    pdksh also parses $EDITOR for vi and switches modes upon startup; I removed that for mksh (especially as I only really maintain the Emacs mode anyway).
    – mirabilos
    Commented Feb 27, 2014 at 14:18
  • Thank you so much addition, especially with the detailed discussion of the behavior and history of various shells. As someone who is regularly required to work in different distros with shells I didn't configure, this is extraordinarily helpful.
    – BryKKan
    Commented Oct 1, 2017 at 19:23
  • 2
    Thanks for the excellent historical context. It's a pity that more sophisticated inline editing features weren't added to the terminal driver at that time. There'd be no need for programs to include libraries such as Readline. I had also wondered why Emacs-mode isn't specified by POSIX so that link to the Rationale was interesting. (I also share your frustration with ^W closing windows). Commented Jun 6, 2018 at 8:47
  • 1
    @AnthonyGeoghegan, we'd still need things like zle/readline as things like filename/command completion could not really be done in the terminal driver. Commented Jun 6, 2018 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .