Question

From a programming standpoint, when you set the bash shell to use vi or emacs via

set -o vi

or

set -o emacs

What is actually going on here? I've been reading a book where it claims the bash shell uses either of these editors for the input to the shell itself, but I thought it may have used readline.

Was it helpful?

Solution

Bash is still using readline. Readline uses either emacs or vi mode and setting the mode switches between the various editor modes. You can check the lib/readline folder in the base source code to see the various key bindings.

OTHER TIPS

According to the man page BASH_BUILTINS(1) (on Fedora 8):

   set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

... (skipping all the single letter options)

         -o option-name
            The option-name can be one of the following:

...

            emacs   Use  an  emacs-style  command  line  editing interface.  This is
                    enabled by default when the shell  is  interactive,  unless  the
                    shell is started with the --noediting option.
...
            vi      Use a vi-style command line editing interface.
I interpret that to mean that bash is directly interpreting the commands for line editing. This option simply sets which command set to use. See the man page for readline(3).

It uses the keystrokes that are familiar to users of one of those editors to edit the command line.

Readline is the facility that provides that feature to Bash and other programs.

From man bash:

READLINE
       This is the library that handles reading input when using  an  interac‐
       tive shell, unless the --noediting option is given at shell invocation.
       Line editing is also used when using the -e option to the read builtin.
       By default, the line editing commands are similar to those of emacs.  A
       vi-style line editing interface is also available.  Line editing can be
       enabled  at  any  time  using  the -o emacs or -o vi options to the set
       builtin (see SHELL BUILTIN COMMANDS below).  To turn off  line  editing
       after  the  shell  is running, use the +o emacs or +o vi options to the
       set builtin.

From what I know, readline is what provides the line-editing functionality for bash.

One proviso: when you press v in vi command mode, you get the full blown vi editor to edit your command line.

From man bash:

READLINE
     This is the library that handles reading input when using an interactive shell, unless the --noediting option is given at shell invocation. By default, the line editing commands are similar to those of emacs. A vi-style line editing interface is also available. To turn off line editing after the shell is running, use the +o emacs or +o vi options to the set builtin.

When the shell presents you with a prompt (unless you're in non-editing mode), you're already using readline. You'll either be in emacs mode or vi insert mode (which is why you can just use ESC to get back to vi command mode).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top