English 中文(简体)
Bash usage of vi or emacs
原标题:

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.

最佳回答

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.

问题回答

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





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

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

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签