views:

162

answers:

4

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.

+1  A: 

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.
Dennis Williamson
ok but for the actual input it uses readline?
Recursion
Yes. "Edit" means input and editing.
Dennis Williamson
+1  A: 

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).

paxdiablo
That's when you press `v` while in **command mode** in `vi` mode.
Dennis Williamson
+2  A: 

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.

miked
`bind -p` will show you the currently active key bindings.
Dennis Williamson
Also note: if you put `set editing-mode vi` (or `set editing-mode emacs`) in your ~/.inputrc file, then other programs that use readline will accept your favorite command line editing actions. When I saw that this gives my python shell readline editing capability I went "ooh aah." Note for Python it only works in the Python shell launched from the command line, not Idle.
iii
+1  A: 

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).

wallyk