Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Readline and inputrc

News Recommended Links Command completion Controlling the Prompt in bash Unix shells history Examples of .bashrc files
Command history reuse Bash and ksh93 Shell Prompts Customarization AIX dotfiles Sysadmin Horror Stories

Humor

Etc

Introduction

The /etc/inputrc file deals with mapping the keyboard for specific situations. This file is the start-up file used by readline, the GNU command line editing library used by Bash, perl and most other open source programs.

For more information, see section Readline Init File in bash man page. The readline man page is also a good source of information. 

Global values are set in /etc/inputrc. Personal (local) user values are set in ~/.inputrc.

The settings in ~/.inputrc file will override the global settings file. Bash use /etc/inputrc if there is no .inputrc for a user when /etc/profile is read (usually at login).

To make the system use both  it is a good idea to place a default .inputrc into the /etc/skel directory for use with new users.

Below in examples you can see several /etc/inputrc files, along with comments to explain what the various options do. Note that comments cannot be on the same line as commands.

To create the initial .inputrc in /etc/skel using the command below, you can write output of bind -p command  to /etc/skel/.inputrc

Be sure to check/set permissions afterward. Copy that file to /etc/inputrc and the home directory of any user already existing on the system, including root, that needs a private version of the file.

You can also copy global file /etc/inputrc to /etc/skel. Be certain to use the -p  parameter of cp command to maintain permissions and be sure to change owner and group appropriately.

More about readline

Readline is actually a library that implements editing of a like of input to command line programs developed for the GNU project. It provides editing and text-manipulation features to make it easier for the user to enter and edit text. Even more importantly it aloow assign commands to keys that are not recognized by initial setting of terminal (up/down home/end etc) That allos more productive use of PC leyuboard. but there are many pitfall is you try to create your own macros.

readline provides default editing in either of two modes: vi or emacs. Both modes provide a subset of the editing commands found in the full editors. 

readline gives bash more flexibility compared to ksh or shells that don't use it, because it can be customized through the use of key bindings, either from the command line or in a special startup file. You can also set readline variables. 

The .inputrc is just readline startup file

The default startup file for readline is called .inputrc. there is hierarchy of those like in case opf profile with /etc/inputrc being global file much like /etc/profile and ~/.inputrc a local, user-defined file similar to ~/.profile. The latter is useful if you wish to customize readline. You can also change the default filename by setting the environment variable INPUTRC 

When bash starts up, it reads first /etc/inputrc and then the local startup file (if there is one). Local file overwrite any similar global setting.

The startup file is just a sequence of lines that bind a keyname to a macro or readline function name. You can also place comments in the file by preceding any line with a #.

You can use either a mnemonic name or a key escape sequence for the keyname. For example, to bind CTRL-T to the movement command for moving to the end of the current line, you could place Control-t: end-of-line in your .inputrc. If you wanted to use a key escape sequence you could have put "\C-t": end-of-line. The \C- is the escape sequence prefix for Ctrl key. The advantage of the key sequence is that you can specify a sequence of keys for an action. In our example, once readline has read this line, typing a Ctrl-T will cause the cursor to move to the end of the line.

The end-of-line in the previous example is a readline function. There are over 60 functions that allow you to control everything from cursor motions to changing text and command completion (for a complete list, see the bash manual page). bind -l will give you a list of readline functions

Here is a more elaborate example:

# Begin /etc/inputrc

# Make sure we don't output everything on the 1 line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On 
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the 
# value contained inside the 1st argument to the 
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# End /etc/inputrc

Creating your own macros

 Bash-2.04 introduced option -x for bind to bind key sequences to shell commands. You can also bind a macro to a key sequence. A macro is simply a sequence of keystrokes inside single or double quotes. Typing the key sequence causes the keys in the macro to be entered as though you had typed them. For example, we could bind text ". ~/.profile" to Ctrl-.

If you want to try out key bindings or you want to see what the current settings are, you can do it from the bash command line by using the bind command. The binding syntax is the same as that of the .inputrc file, but you have to surround each binding in quotes so that it is taken as one argument.

To bind a string to CTRL-l, we could type

 bind -x '"\C-l": ll'

this is and example how bind -x can  useful  as it binds CTRL-L to the ll alias. Hitting CTRL-L would then give a directory listing.

bind also allows you to print out the bindings currently in effect by typing and grep those which are interested you (total list is quite big)

bind -P | grep -v 'not bound'

Another option you might find useful is -p. This prints out the bindings to standard output in a format that can be re-read by bind, or used as a .inputrc file. So, to create a complete .inputrc file that you can then edit, you could type

bind -p > .inputrc

But you need to be brave and persistent to achive success here.

If you do so, you'll see things like:

...

If you just want to see the names of the readline functions, you can use bind -l.

Unbinding

You can also unbind a function by using bind -u along with the name of the function; all keys for that function will then be unbound. Unbinding a key sequence can be done with bind -r followed by the sequence.

You can also wipe out your crurrent setting by readin the file .inputrc back. In this case you need to use option, -f. This option takes a filename as its argument and reads the key bindings from that file. You can also use it to update the key bindings if you've just modified your .inputrc.

To re-read the file and apply or test after making changes, use bind-f filename.

You'd like to adjust the way bash handles input, especially command completion. For example, you'd like it to be case-insensitive.

Edit or create a ~/.inputrc or /etc/inputrc file as appropriate. There are many parameters you can adjust to your liking. To have readline use your file when it initializes, set $INPUTRC; for example, set INPUTRC='~/.inputrc'.

If you want to use single or double quotes in your macros, you can escape them by using a backslash (\).

Escape sequences
Sequence Description
\C- Control key prefix
\M- Meta (Escape) key prefix
\e The escape character
\\ The backslash character (\)
\" The double quote character (")
\' The single quote character (')
 

Conditionals

readline also allows simple conditionals in the .inputrc. There are three directives: $if, $else, and $endif. The conditional of the $if can be an editing mode, a terminal type, or an application-specific condition.

To test for an editing mode, you can use the predicate mode=emacs . For example:

$if mode=emacs
bind -x "\C-t":end-of-line'
$endif

Likewise, to test for a terminal type, you can use the form term=. You must provide the full terminal name on the right-hand side of the test. This is useful when you need a terminal-specific key binding. You may, for instance, want to bind the function keys of a particular terminal type to key sequences.

If you have other applications that use readline, you might like to keep your bash-specific bindings separate. You can do this with the last of the conditionals. Each application that uses readline sets its own variable, which you can test for. To test for bash specifics, you could put $if bash into your .inputrc.

Mapping function keys

One interesting possibility provided by readline is to map commands to function keys (F1-F12)

Here is how to map ll command to F3

bind '"\e[13~":'$'"\201"'; 
bind -x $'"\201":ll'

Note: bash support of F-keys remapping is buggy and you need first map F1 to non-existent character 201 and then 201 to ll. See asigning a certain command to a certain keyboard key - comp.unix.shell Google Groups

Readline variables

readline has its own set of variables/options that you can set from within your .inputrc.  

Variable Description
bell-style If set to none, readline never rings the bell (beeps). If set to visible, readline will attempt to use a visible bell. If set to audible, it will attempt to ring the bell. The default is audible.
comment-begin The string to insert when the readline insert-comment command is executed. The default is a #.
completion-query-items Determines when the user is asked to see further completions if the number of completions is greater than that given. The default is 100.
convert-meta If set to On, converts characters with the eighth bit set to an ASCII key sequence by stripping the eighth bit and prepending an escape character. The default is On.
disable-completion If set to On, inhibits word completion. Completion characters will be inserted into the line as if they had been mapped to self-insert. The default is Off.
editing-mode Sets the editing mode to vi or emacs.
enable-keypad If set to On, readline tries to enable the keyboard's application keypad when it is called. Some systems need this to enable the arrow keys. The default is Off.
expand-tilde If set to On, tilde expansion is attempted when readline attempts word completion. The default is Off.
horizontal-scroll-mode Set to On means that lines will scroll horizontally if you type beyond the right-hand side of the screen. The default is Off, which wraps the line onto a new screen line.
input-meta If set to On, eight-bit input will be accepted. The default is Off. This is synonymous with meta-flag.
keymap Sets readline's current keymap for bindings. Acceptable names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command and vi-insert. The default is emacs. Note that the value of editing-mode also affects the keymap.
mark-directories If set to On, completed directory names have a slash appended.
mark-modified-lines If set to On, displays an asterisk at the start of history lines that have been modified. The default is Off.
meta-flag If set to On, eight-bit input will be accepted. The default is Off.
output-meta If set to On, displays characters with the eighth bit set directly. The default is Off.
show-all-if-ambiguous If set to On, words with more than one possible completion are listed instead of ringing the bell. The default is Off.
visible-stats If set to On, a character denoting a file's type as reported by the stat system call is appended to the filename when listing possible completions. The default is Off.
 

To set any of the variables, you can use the set command in your .inputrc. For example, to set vi-mode when you start up, you could place the line set editing-mode vi in your .inputrc. Every time bash starts it would change to vi-mode.

Exploring bind command

We recommend you explore the bind command and the readline documentation, especially bind -v, bind -l, bind -s, and bind -p.

Try the following command for

bind -P a list of keys and their bindings 
bind -l will give you a list of readline functions. 
bind -p will give you a list in the format for .inputrc so you can do:- 
bind -p > .inputrc # please not use that without daving original .inputrc first. 
           # It produces a lot of extra binding that essentially destroy your .inputrc

Among potentially useful default shortcuts: 

Here is the relevant fragment of Bash Reference Manual - Bash Builtins manual that describes bind. This section describes built-in commands which are unique to or have been extended in Bash.  Not everything works as described :-(. You need to experiment and what what works what not.

bind
bind [-m keymap] [-lpsvPSV]
bind [-m keymap] [-q function] [-u function] [-r keyseq]
bind [-m keymap] -f filename
bind [-m keymap] keyseq:function-name
Display current Readline (see section Command Line Editing) key and function bindings, or bind a key sequence to a Readline function or macro. The binding syntax accepted is identical to that of `.inputrc' (see section Readline Init File), but each binding must be passed as a separate argument: e.g., `"\C-x\C-r":re-read-init-file'. Options, if supplied, have the following meanings:
-m keymap
Use keymap as the keymap to be affected by the subsequent bindings. Acceptable keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-command, and vi-insert. vi is equivalent to vi-command; emacs is equivalent to emacs-standard.
-l
List the names of all Readline functions.
-p
Display Readline function names and bindings in such a way that they can be re-read.
-P
List current Readline function names and bindings.
-v
Display Readline variable names and values in such a way that they can be re-read.
-V
List current Readline variable names and values.
-s
Display Readline key sequences bound to macros and the strings they output in such a way that they can be re-read.  Does not work in RHEL 6.5. for example try
bind -s | fgrep '\C-'
-S
Display Readline key sequences bound to macros and the strings they output.
-f filename
Read key bindings from filename.
-q function
Query about which keys invoke the named function.
-u function
Unbind all keys bound to the named function.
-r keyseq
Remove any current binding for keyseq.
 

Examples

Basic staff:

bind -P # a list of keys and their bindings 
bind -l # gives you a list of readline functions. 
bind -p # gives you a list in the format for .inputrc so you can write it to a file
          bind -p > .inputrc 

Example from /etc/inputrc [Homepage von Robert Kehl]

# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, comment out
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
#set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion 
# set bell-style none 

# Show all if ambigious.
set show-all-if-ambiguous on
# bash completions does not use more
set page-completions off

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# those two are for rxvt
#"\e[7~":beginning-of-line
#"\e[8~":end-of-line
# on some xterm
#"\e[H": beginning-of-line
#"\e[F": end-of-line
# on nxterms
#"\e[\C-@": beginning-of-line
#"\e[e": end-of-line

# allow the use of the Delete/Insert keys
# "\e[2~": quoted-insert
"\e[2~": yank                   # Insert
# "\e[3~": delete-char
"\e[3~":delete-char             # Suppr

# mappings for "page up" and "page down" to step to the beginning/end 
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history
# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

# # mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
#"\e[5C": forward-word
#"\e[5D": backward-word
#"\e\e[C": forward-word
#"\e\e[D": backward-word

# $if term=rxvt
# "\e[8~": end-of-line
# $endif

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
# "\eOH": beginning-of-line
# "\eOF": end-of-line

# for freebsd console
# "\e[H": beginning-of-line
# "\e[F": end-of-line

$if term=xterm

# xterm with NumLock ON
# Operators
#"\eOo":         "/"
#"\eOj":         "*"
#"\eOm":         "-"
#"\eOk":         "+"
#"\eOl":         "+"
#"\eOM":         accept-line

# Colon and dot
#"\eOl":       ","
"\eOn":         "."

# Numbers
"\eOp":         "0"
"\eOq":         "1"
"\eOr":         "2"
"\eOs":         "3"
"\eOt":         "4"
"\eOu":         "5"
"\eOv":         "6"
"\eOw":         "7"
"\eOx":         "8"
"\eOy":         "9"

# Application keypad and cursor of xterm
"\eOD":         backward-char
"\eOC":         forward-char
"\eOA":         previous-history
"\eOB":         next-history
"\eOE":         re-read-init-file

$endif

$endif

Robert Kehl

# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, comment out
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
#set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion 
# set bell-style none 

# Show all if ambigious.
set show-all-if-ambiguous on
# bash completions does not use more
set page-completions off

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# those two are for rxvt
#"\e[7~":beginning-of-line
#"\e[8~":end-of-line
# on some xterm
#"\e[H": beginning-of-line
#"\e[F": end-of-line
# on nxterms
#"\e[\C-@": beginning-of-line
#"\e[e": end-of-line

# allow the use of the Delete/Insert keys
# "\e[2~": quoted-insert
"\e[2~": yank                   # Insert
# "\e[3~": delete-char
"\e[3~":delete-char             # Suppr

# mappings for "page up" and "page down" to step to the beginning/end 
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history
# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

# # mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
#"\e[5C": forward-word
#"\e[5D": backward-word
#"\e\e[C": forward-word
#"\e\e[D": backward-word

# $if term=rxvt
# "\e[8~": end-of-line
# $endif

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
# "\eOH": beginning-of-line
# "\eOF": end-of-line

# for freebsd console
# "\e[H": beginning-of-line
# "\e[F": end-of-line

$if term=xterm

# xterm with NumLock ON
# Operators
#"\eOo":         "/"
#"\eOj":         "*"
#"\eOm":         "-"
#"\eOk":         "+"
#"\eOl":         "+"
#"\eOM":         accept-line

# Colon and dot
#"\eOl":       ","
"\eOn":         "."

# Numbers
"\eOp":         "0"
"\eOq":         "1"
"\eOr":         "2"
"\eOs":         "3"
"\eOt":         "4"
"\eOu":         "5"
"\eOv":         "6"
"\eOw":         "7"
"\eOx":         "8"
"\eOy":         "9"

# Application keypad and cursor of xterm
"\eOD":         backward-char
"\eOC":         forward-char
"\eOA":         previous-history
"\eOB":         next-history
"\eOE":         re-read-init-file

$endif

$endif

Example of inputrc  from Chris Lynn <[email protected]>

cat > /etc/inputrc <<- "EOF"
# Begin /etc/inputrc
# Modified by Chris Lynn <[email protected]>

# Make sure we don't output everything on the 1 line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On 
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the 
# value contained inside the 1st argument to the 
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for Konsole
"\e[H": beginning-of-line
"\e[F": end-of-line

# End /etc/inputrc 
EOF
Werner Fink /etc/inputrc from Suse
################################################################################
## /etc/inputrc
##
## Attempt to put different TERMs together in one readline init file.
## Copyright (c) 1997,2000,2002 SuSE Linux AG, Nuernberg, Germany.
##
## Author: Werner Fink
## Please send feedback to http://www.suse.de/feedback
##
################################################################################
#
# Eight bit compatible: Umlaute
#
set meta-flag on
set output-meta on
set convert-meta off
set term xy
#
# VI line editing
#
$if mode=vi
set editing-mode vi
set keymap vi
$endif
#
# Common standard keypad and cursor
# F01-F16
"\e[1~":        beginning-of-line
"\e[2~":        yank
"\e[3~":        delete-char
"\e[4~":        end-of-line
"\e[5~":        history-search-backward
"\e[6~":        history-search-forward
... ...
#
# TERM=linux or console or gnome
#
"\e[1~":        beginning-of-line
"\e[4~":        end-of-line
... ... ...
#	Function	keys	F1	-	F12
#
$if	term=linux
#
#	On	console	the	first	five	function	keys
#
"\e[[A":	prefix-meta
"\e[[B":	undo
"\e[[C":	""
"\e[[D":	kill-line
"\e[[E":	""
... ... ...
#
# Standard cursor
#
"\e\e[D":       backward-word
"\e\e[C":       forward-word
"\e\e[A":       up-history
"\e\e[B":       down-history
"\C-\e[D":      backward-char
"\C-\e[C":      forward-char
"\C-\e[A":      up-history
"\C-\e[B":      down-history

Tips and Tricks

Stephane CHAZELAS

 More options Nov 24 2003, 6:06 am

Newsgroups: comp.unix.shell

From: Stephane CHAZELAS <this.addr...@is.invalid>

Date: Mon, 24 Nov 2003 10:36:43 +0100

Local: Mon, Nov 24 2003 5:36 am

Subject: Re: asigning a certain command to a certain keyboard key

Reply to author | Forward | Print | View thread | Show original | Report this message | Find messages by this author

2003-11-23, 17:56(-08), John Green:
 

 

>> bash key mapping handling is mostly bogus. You may have more chance with:
 

>> bind -x $'"\201":clear'
>> bind '"\eOP":'$'"\201"'
 

>> Troubles begin as soon as there's one binding for a char sequence, that gets even worse with "-x".
 

> Thanks, that worked.
 

> Now I need to re-read 'man bash' very carefully to try to understand why.

That's just a two step mapping.

First "clear" is mapped to the single character \201 (a non-valid character in iso-8859-***, so there are few chances that you may type it on keyboard) in the -x keymap, then <F1> is
mapped to that character.

The bug it works around is not described in the man page. There are many others regarding key binding. It probably needs to be reworked totally.

Marc Liyanage - Blog - Mac OS X - Terminal Tricks Bash directory helper key bindings 

Bash-2.04 introduced the following new features:

o Programmable word completion with the new `complete' and `compgen' builtins;
  examples are provided in examples/complete/complete-examples
o `history' has a new `-d' option to delete a history entry
o `bind' has a new `-x' option to bind key sequences to shell commands

Find and bind key sequences in bash

Difference in RedHat Linux 9.0
I use RedHat Linux 9.0 and discovered that bind requires an additional switch -x to tell the system you are keymapping.

I use pine for email and use

bind -x '"\ep":pine' # not does not work --NNB

so I can press ALT-P to start pine!

Adding this to my .bashrc file in my logon directory makes this command automatic so I don't have to type it each time I log on.

Find and bind key sequences in bash

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

Most keyboards today come with an extra row of function keys at the top of the keyboard. These function keys can be customized and used within bash, or any other shell, by binding the key sequences to a command in the shell. Some keys may be intercepted by the window manager or the terminal program, such as konsole or gnome-terminal. You can retain those key bindings and use unassigned keys inside the shell, or you can reconfigure them to use a certain key in the shell instead.

To obtain the key sequence from a function key, use the read command. The following is an example of pressing the [F12] key:

$ read
^[[24~

Note that different keyboards will produce different key sequences, and modifiers to the function keys (such as [Ctrl][F12] or [Shift][F12]) will produce other sequences as well.

The next step is to bind that key sequence to a particular shell command. For example, you can bind [F12] to the "history-search-backward" shell command:

$ bind '"\e[24~": history-search-backward'

Make sure you write the key sequence as \e[24~ rather than ^[[24~. This is because the ^[ sequence is equivalent to the [Esc] key, which is represented by \e in the shell. So, for instance, if the key sequence was ^[[OP the resulting bind code to use would be \e[OP.

Not only does the bind command bind function keys, but you can also use bind to map key sequences (such as [Esc][P] or [Esc][Q]) by writing the bind key code as \ep and \eq respectively.

For a list of shell commands that you can use, examine the /etc/inputrc file

Bind extended keys to BASH functions

BASH's bind command permits three activities -- definition of new macros, definition of new key bindings for existing commands, and dumping the installed keybindings.

Macro is a string of characters  binded to a key combination. A typical textbook example would be:

lotzmana@safe$ bind '"\M-k"':"\"ls -f\""

It pastes "ls -f" when you press Alt+K. Later in life one usually finds that engaging the pre-existing keybinginds provides for some more inventive use of bind. More elaborate example follows.

Have you ever found yourself at the end of very long command just to remember that something else had to be executed before that, usually this means ctrl+c and more typing and retyping. The designers of the beloved 4NT thoughtfully provided ctrl+k to save the current line into history instead of executing it. Can't we forge something similar in bash?

lotzmana@safe$ bind '"\M-k"':"\"\C-ahistory -s '\C-e'\C-m\"" 

It binds a macro to Alt+K. The macro is not a plain text to be pasted but invokes some editing commands already binded to other keys. Imagine that you have already typed a long line, to save it you press ctrl+a (this moves the cursor to the beginning of the line), type "history -s ", press ctrl+e to go to the end and Enter (or ctrl+m) to execute. This is precisely the sequence of actions encoded in the macro binded to Alt+K.

PgUp in 4NT invokes a popup menu with the most recent commands for easy selection. While GUI programming is not an option, bind can still bring us closer to civilized life:

lotzmana@safe$ bind '"\e[5~"':"\"history | tail -25\C-m\""

Explore the manpages of readline and bash for more details and options.

Binding to keys that have no name definitions -- extended keys

The binding to PgUp didn't use some key name but employed the extended ESC sequence which PgUp generates. On some terminals using the meta encoding, like M-k, might also not work. It just happens that there is no accepted standard for how functional keys are encoded. This might be an issue for the deployment of one single .bashrc in heterogenous OS/terminal environment.

For example on my xterm I have to define the macro like this:

lotzmana@safe$ bind  '"\xeb"':"\"\C-ahistory -s '\C-e'\C-m\"" 

How did I know that 0xeb is the character that Alt+K generates? Well, first approximation was to use the debug terminal of the WW multi-platform console editor. The keyboard handler has a debug mode where it prints all the character sequences it couldn't match to known key combinations. Because this editor is not yet very popular I exported the knowledge into a handy standalone perl script. Start the script, press Alt+K, press 'x':

lotzmana@safe$ perl keys.pl
press 'x' to exit
\xeb
restore console mode

Then use the hex sequence on the left-hand side of bind.

Moving back in time  to the niceties of 4NT I remember that ctrl+left-arrow/ctrl+right-arrow moved cursor to the beginning of the previous or the next word. This is also available in bash -- Alt+F/Alt+B, but why not have the real thing?! Here are the key sequences I need under xterm:

lotzmana@safe$ perl keys.pl
press 'x' to exit
\x1b\x5b\x31\x3b\x35\x44
\x1b\x5b\x31\x3b\x35\x43
restore console mode
lotzmana@safe$ bind '"\e\x5b\x31\x3b\x35\x44"':backward-word
lotzmana@safe$ bind '"\e\x5b\x31\x3b\x35\x43"':forward-word

List of useful keybindings of bash under xterm

In the course of the last few weeks I assembled a list of a few key definition bindings which I find useful. They either assign functions to new keys or link up function previously inaccessible because of differences in key definitions on xterm. Best is to add this to your .bashrc:

# Now map xterm's alternative keybindings to existing functionality
# Some are simple translations to correspontend M- combinations
# ctrl+left/right arrows:
bind '"\e\x5b\x31\x3b\x35\x44"':backward-word
bind '"\e\x5b\x31\x3b\x35\x43"':forward-word
# alt+b/f:
bind '"\xe2"':'"\M-b"'
bind '"\xe6"':'"\M-f"'
# atl+backspace:
bind '"\xff"':backward-kill-word
# alt+'.':
bind '"\xae"':yank-last-arg
# alt+k:
bind '"\xeb"':"\"\M-k\""
# alt+w:
bind '"\xf7"':'"\M-w"'

Notice the way Alt+k is mapped to M-k, it is handy when you don't immediately know the name of the function you are assigning to, but only know the original keybinding. If you log in under other terminals you must use the keys.pl script to figure the keysequences of the keys you wish to bind there and add them to your .bashrc.

Inside keys.pl

You can simply download keys.pl and begin using it. If you are curious how it works, look at the source below and the explanation notes after that:
lotzmana@safe$ nl -w2 -s' '  keys.pl    
 1 #!/usr/bin/perl -w
 2 # keys.pl 1.0.0, 13-jul-2005
 3 # petar marinov, http:/geocities.com/h2428, this is public domain
   
 4 use strict;
 5 $| = 1;
 6 my $got = '';
 7 while (1) {
 8   # wait for a sequence to begin
 9   $got = getone() while (ord($got) == 0);
10   # process a sequence ($got already holds the first character)
11   while (ord($got) != 0) {
12     exit(0) if ($got eq 'x');
13     printf("\\x%02x", ord($got));
14     $got = getone();
15   }
16   print "\n";
17 }
18 exit(0);
   
19 BEGIN {
20   use POSIX qw(:termios_h);
21   use Fcntl;
   
22   my ($term, $oterm, $echo, $noecho);
23   $term = POSIX::Termios->new();
24   $term->getattr(fileno(STDIN));
25   $oterm = $term->getlflag();
   
26   # puts console in a raw mode -- permits non-blocking reading
27   sub raw {
28     my $flags = 0;
29     $term->setlflag($oterm & ~(ECHO | ECHOK | ICANON));
30     $term->setcc(VTIME, 1);
31     $term->setattr(fileno(STDIN), TCSANOW);
32     fcntl(STDIN, F_GETFL, $flags)
33       or die "Couldn't get flags for HANDLE : $!\n";
34     $flags |= O_NONBLOCK;
35     fcntl(STDIN, F_SETFL, $flags)
36       or die "Couldn't set flags for HANDLE: $!\n";
37   }
   
38   # restores console to original mode
39   sub cooked {
40     print "restore console mode\n";
41     $term->setlflag($oterm);
42     $term->setcc(VTIME, 0);
43     $term->setattr(fileno(STDIN), TCSANOW);
44   }
   
45   # reads character or times out
46   sub getone {
47     my $key = ' ';
48     my ($rv, $rin, $win, $ein);49     my ($nfound, $timeleft, $rout, $wout, $eout);
   
50     $rin = $win = $ein = '';
51     vec($rin, fileno(STDIN), 1) = 1;
52     $ein = $rin | $win;
53     ($nfound,$timeleft) =
54       select($rout=$rin, $wout=$win, $eout=$ein, 0.10);
55     return chr(0) if $nfound == 0;
56     $rv = sysread(STDIN, $key, 1);
57     return chr(0) if (!defined($rv));
58     return $key;
59   }
   
60   raw();
61   print "press 'x' to exit\n";
62 }
   
63 END {
64   cooked() 
65 }

7-17: The main loop consiste of two sub loops. The first first loop (line 9) waits for a new sequence  to begin. The second extracts all keys of a sequence and prints them on the screen.
 

26-37: For a program to read characters in a non-blocking mode it has to first switch the terminal to raw mode -- echo off, non-canonical, time-out set to minimum, and non-blocking mode read for STDIN.
 

45-59: The non-blocking read function uses select() to wait with timeout for a character from STDIN. It returns 0 on time out or ascii when character is present.
 

64-65: It politely returns the terminal back to mode which is good for command line editing.


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Mar 10, 2008] Problem with "bind -x"

This is an undocumented bug in bash that exists for the last five years or more. See above for the tick to bypass it

I'm having trouble with the bind builtin. I'm working on a trivial example
to bind a shell command to a key sequence. If I type

bind -x '"abc":/bin/echo def'

I would expect the screen to display "def", but instead I get this error:

bash2: bash_execute_unix_command: cannot find keymap for command

Granted that this is a trivial example, what am I doing wrong, and how can
I make it work? The eventual goal is to use bind to map function keys to
aliases.

--
"The only thing that helps me maintain my slender grip on reality is the
friendship I share with my collection of singing potatoes."

- Holly, JMC Vessel *Red Dwarf*

[Mar 10, 2008 GeSHi - Generic Syntax Highlighter Home

Can be used for shell scripts and bash initrc

[Mar 10, 2008 Bash Config Files

How Bash executes startup files.

For Login shells (subject to the -noprofile option):

On logging in:
If `/etc/profile' exists, then source it.

If `~/.bash_profile' exists, then source it,
else if `~/.bash_login' exists, then source it,
else if `~/.profile' exists, then source it.

On logging out:
If `~/.bash_logout' exists, source it.

For non-login interactive shells (subject to the -norc and -rcfile options):
On starting up:
If `~/.bashrc' exists, then source it.

For non-interactive shells:
On starting up:
If the environment variable `ENV' is non-null, expand the variable and source the file named by the value.
If Bash is not started in Posix mode, it looks for `BASH_ENV' before `ENV'.

So, typically, your `~/.bash_profile' contains the line
`if [ -f `~/.bashrc' ]; then source `~/.bashrc'; fi' after (or before) any login specific initializations.

If Bash is invoked as `sh', it tries to mimic the behavior of `sh' as closely as possible. For a login shell, it attempts to source only `/etc/profile' and `~/.profile', in that order. The `-noprofile' option may still be used to disable this behavior. A shell invoked as `sh' does not attempt to source any other startup files.

When Bash is started in POSIX mode, as with the `-posix' command line option, it follows the Posix 1003.2 standard for startup files. In this mode, the `ENV' variable is expanded and that file sourced; no other startup files are read.

My .bashrc can be found here.

My .bash_profile can be found here.

.inputrc (readline)-- Although the Readline library comes with a set of Emacs-like key bindings installed by default, it is possible that you would like to use a different set of keybindings. You can customize programs that use Readline by putting commands in an "init" file in your home directory. The name of this file is taken from the value of the shell variable `INPUTRC'. If that variable is unset, the default is `~/.inputrc'.

When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.

In addition, the `C-x C-r' command re-reads this init file, thus incorporating any changes that you might have made to it.

Conditional Init Constructs within readline

Readline implements a facility similar in spirit to the conditional compilation features of the C preprocessor which allows key bindings and variable settings to be performed as the result of tests. There are three parser directives used.

`$if' The `$if' construct allows bindings to be made based on the editing mode, the terminal being used, or the application using Readline. The text of the test extends to the end of the line; no characters are required to isolate it.
`mode' The `mode=' form of the `$if' directive is used to test whether Readline is in `emacs' or `vi' mode. This may be used in conjunction with the `set keymap' command, for instance, to set bindings in the `emacs-standard' and `emacs-ctlx' keymaps only if Readline is starting out in `emacs' mode.
`term' The `term=' form may be used to include terminal-specific key bindings, perhaps to bind the key sequences output by the terminal's function keys. The word on the right side of the `=' is tested against the full name of the terminal and the portion of the terminal name before the first `-'. This allows SUN to match both SUN and SUN-CMD, for instance.
`application' The APPLICATION construct is used to include application-specific settings. Each program using the Readline library sets the APPLICATION NAME, and you can test for it. This could be used to bind key sequences to
functions useful for a specific program.
`$endif' This command terminates an `$if' command.
`$else' Commands in this branch of the `$if' directive are executed if the test fails.

The following command adds a key sequence that quotes the current or previous word in Bash:
$if bash
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
$endif

My .inputrc file is here

Last update by Hermann Heimhardt on October 7, 2001

[Feb 26, 2008] 7.8. Creating the -etc-inputrc File

The inputrc file handles keyboard mapping for specific situations. This file is the startup file used by Readline - the input-related library - used by Bash and most other shells.

Most people do not need user-specific keyboard mappings so the command below creates a global /etc/inputrc used by everyone who logs in. If you later decide you need to override the defaults on a per-user basis, you can create a .inputrc file in the user's home directory with the modified mappings.

For more information on how to edit the inputrc file, see info bash under the Readline Init File section. info readline is also a good source of information.

Below is a generic global inputrc along with comments to explain what the various options do. Note that comments cannot be on the same line as commands. Create the file using the following command:

cat > /etc/inputrc << "EOF"
# Begin /etc/inputrc
# Modified by Chris Lynn <[email protected]>

# Allow the command prompt to wrap to the next line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the
# value contained inside the 1st argument to the
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for Konsole
"\e[H": beginning-of-line
"\e[F": end-of-line

# End /etc/inputrc
EOF

System Administration Toolkit Get the most out of bash

Command-line editing and key bindings

The main command prompt within bash provides both the ability to edit the command line and a history function, remembering individual command lines so that you can execute them again.

The editing functionality means that you can go forward and backward through the command line currently displayed to make changes and correct typos. In bash's standard form, you can use the cursor keys for basic movement. More extensive commands, such as going backward and forward by words, are controlled by the Readline library that supports both vi and emacs bindings by default. To set the editing mode, specify your preferred mode either on the command line or in a bootstrap file: $ set editing-mode emacs.

For example, using the emacs editing mode, the following key bindings are in effect:

You can in fact bind any key or combination you like to a particular operation using the internal bind bash command. To start, you can get a list of the available commands by using the -P option (see Listing 1).


Listing 1. Using the -P option to get a list of available commands
$ bind -P 

abort can be found on "\C-g", "\C-x\C-g", "\M-\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys
backward-byte is not bound to any keys
...
yank can be found on "\C-y".
yank-last-arg can be found on "\M-.", "\M-_".
yank-nth-arg can be found on "\M-\C-y".
yank-pop can be found on "\M-y".

The \C refers to the control key. The \M sequence refers to the 'meta' key (special on some keyboards, or usually the Alt key or the Escape key).

To set a binding, you must specify the key sequence and the command to be executed, separated by a colon, with the key sequence escaped by a double quote (in extreme circumstances, you might need to escape this again with a single quote). For example, to change Control-B to go backwards word by word, use $ bind "\C-b":backward-word.

You can even use the binding to execute a shell command (for example, to run an application). To do this, you must add the -x option, and this is an example of where the escaping of both is required. For example, to set Control-E to run emacs, you would use the following: $ bind -x '"\C-e"':emacs.

To have key bindings in bash enabled every time, you can either set the information in the .inputrc file (which then affects all Readline-enable d applications), or you can place specific bash bindings in your startup scripts, which will be covered later in this article.

inputrc

# this eliminates the need for hitting tab twice to show choices whenever there are multiple completion matches set show-all-if-ambiguous on
-etc-inputrc [Homepage von Robert Kehl]
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, comment out
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
#set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion 
# set bell-style none 

# Show all if ambigious.
set show-all-if-ambiguous on
# bash completions does not use more
set page-completions off

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# those two are for rxvt
#"\e[7~":beginning-of-line
#"\e[8~":end-of-line
# on some xterm
#"\e[H": beginning-of-line
#"\e[F": end-of-line
# on nxterms
#"\e[\C-@": beginning-of-line
#"\e[e": end-of-line

# allow the use of the Delete/Insert keys
# "\e[2~": quoted-insert
"\e[2~": yank                   # Insert
# "\e[3~": delete-char
"\e[3~":delete-char             # Suppr

# mappings for "page up" and "page down" to step to the beginning/end 
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history
# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

# # mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
#"\e[5C": forward-word
#"\e[5D": backward-word
#"\e\e[C": forward-word
#"\e\e[D": backward-word

# $if term=rxvt
# "\e[8~": end-of-line
# $endif

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
# "\eOH": beginning-of-line
# "\eOF": end-of-line

# for freebsd console
# "\e[H": beginning-of-line
# "\e[F": end-of-line

$if term=xterm

# xterm with NumLock ON
# Operators
#"\eOo":         "/"
#"\eOj":         "*"
#"\eOm":         "-"
#"\eOk":         "+"
#"\eOl":         "+"
#"\eOM":         accept-line

# Colon and dot
#"\eOl":       ","
"\eOn":         "."

# Numbers
"\eOp":         "0"
"\eOq":         "1"
"\eOr":         "2"
"\eOs":         "3"
"\eOt":         "4"
"\eOu":         "5"
"\eOv":         "6"
"\eOw":         "7"
"\eOx":         "8"
"\eOy":         "9"

# Application keypad and cursor of xterm
"\eOD":         backward-char
"\eOC":         forward-char
"\eOA":         previous-history
"\eOB":         next-history
"\eOE":         re-read-init-file

$endif

$endif 

cat > /etc/inputrc << "EOF"

# Begin /etc/inputrc
# Modified by Chris Lynn <[email protected]>

# Make sure we don't output everything on the 1 line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On 
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the 
# value contained inside the 1st argument to the 
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for Konsole
"\e[H": beginning-of-line
"\e[F": end-of-line

# End /etc/inputrc
EOF
 

.inputrc and command line edit mode function key remapping


Think of this as a brief FUQ (a veritable 'Quickie', as
it were) -- Frequently Unanswered Questions

Disclaimer/Alibi
----------------
These are really questions for the 'bash' group, except
that they specifically deal with the PC and its
keyboard (*and* I'm using 'bash' on Cygwin).

I've searched for answers throughout the Cygwin,
'emacs' and 'bash' documentation, and the Web and even
bought the book, /Learning the bash shell/. Alas, no
FUQing help! ;-)

I plan to document this in some copylefted docs, so you
should be able to leverage the time of your answers.

Q1 -- When you remap a 'bash' Edit Mode function in
.inputrc, it looks like this:

"\e[3~":      delete-char # DEL key

The entity in double quotes ("\e[3~"), I'm calling the
"key ID (KID)". In the above '.inputrc' declaration,
the function 'delete-char' being remapped from its
default key assignment to the KID -- "\e[3~" -- the
<DEL> key.

What are the KIDs of the following IBM PC keys
(specified below with facsimiles of the key caps
contained in angle brackets -- '<...>')?

Cursor control key pad
----------------------
<HOME>
<END>
<left-arrow>
<right-arrow>
<PAGE_UP>
<PAGE_DOWN>
<INSERT>

Numeric pad
-----------
<->
<+>
<ENTER>
</>

In general, I'd like a table that maps the KIDs for all
104 keys on the keyboard I use. Or, better still, is
there a way to use scan codes?

(Incidentally, what makes finding a table of these
KIDs so difficult is the failure of the documentation
to assign this concept a unique, or even a consistent
word.)

Q2
--

Is there a way to make the <INSERT> key a toggle
between the insert and overwrite modes of 'bash'
edit mode?

I used to have these figured out for 'Microemacs', but
that was half a lifetime ago, for me, and Microemacs
supported scan codes, if I remember correctly.

The discerning reader will have surmised both from
these questions and my earlier query 'on the humble
<DEL> key', that I am trying to make a PC, under the
estimable Cygwin, exploit those few niceties of the PC
(keyboard).

Thanks

Lee

P.S. In anticipation of the "righteous" among you being
     offended by the term 'FUQ', I shared your
     indignation; it occurred shortly after buying the
     ill-fated -- and referenced -- 'bash' book. (Yes,
     it's true; I'm bashing the 'bash' book.)

     And, please, whatever you do, don't kid a KIDer. ;-)


Lee D. Rothstein -- [email protected]
VeriTech -- 603-424-2900
7 Merry Meeting Drive
Merrimack, NH 03054-2934

----------

problems with certain entries in .inputrc


I'm using Cygwin 1.1.7 for Win95x:

BUG: If I include the following line in my .inputrc file, then no bindings
get loaded when the shell starts:

"Û": self-insert

(the above is ascii value 219)

what is wierd is that all other binding directives work fine.

This is the contents of my .inputrc file.  You'll notice that I commented
out the trouble line - when I do this, all bindings work fine.  However, if
I uncomment this line, then none of my bindings work:

"\C-g": abort
"\C-x\C-g": abort
"\e\C-g": abort
"\C-j": accept-line
"\C-m": accept-line
# alias-expand-line (not bound)
# arrow-key-prefix (not bound)
"\C-b": backward-char
"\eOD": backward-char
"\e[D": backward-char
"\C-h": backward-delete-char
"\C-?": backward-delete-char
"\C-x\C-?": backward-kill-line
"\e\C-h": backward-kill-word
"\e\C-?": backward-kill-word
"\eb": backward-word
"\e<": beginning-of-history
"\C-a": beginning-of-line
"\e[1~": beginning-of-line
"\C-xe": call-last-kbd-macro
"\ec": capitalize-word
"\C-]": character-search
"\e\C-]": character-search-backward
"\C-l": clear-screen
"\C-i": complete
"\e\C-[": complete
"\e!": complete-command
"\e/": complete-filename
"\e@": complete-hostname
"\e{": complete-into-braces
"\e~": complete-username
"\e$": complete-variable
# copy-backward-word (not bound)
# copy-forward-word (not bound)
# copy-region-as-kill (not bound)
"\C-d": delete-char
# delete-char-or-list (not bound)
"\e\\": delete-horizontal-space
"\e-": digit-argument
"\e0": digit-argument
"\e1": digit-argument
"\e2": digit-argument
"\e3": digit-argument
"\e4": digit-argument
"\e5": digit-argument
"\e6": digit-argument
"\e7": digit-argument
"\e8": digit-argument
"\e9": digit-argument
"\C-x\C-v": display-shell-version
"\C-xA": do-lowercase-version
"\C-xB": do-lowercase-version
"\C-xC": do-lowercase-version
"\C-xD": do-lowercase-version
"\C-xE": do-lowercase-version
"\C-xF": do-lowercase-version
"\C-xG": do-lowercase-version
"\C-xH": do-lowercase-version
"\C-xI": do-lowercase-version
"\C-xJ": do-lowercase-version
"\C-xK": do-lowercase-version
"\C-xL": do-lowercase-version
"\C-xM": do-lowercase-version
"\C-xN": do-lowercase-version
"\C-xO": do-lowercase-version
"\C-xP": do-lowercase-version
"\C-xQ": do-lowercase-version
"\C-xR": do-lowercase-version
"\C-xS": do-lowercase-version
"\C-xT": do-lowercase-version
"\C-xU": do-lowercase-version
"\C-xV": do-lowercase-version
"\C-xW": do-lowercase-version
"\C-xX": do-lowercase-version
"\C-xY": do-lowercase-version
"\C-xZ": do-lowercase-version
"\eA": do-lowercase-version
"\eB": do-lowercase-version
"\eC": do-lowercase-version
"\eD": do-lowercase-version
"\eE": do-lowercase-version
"\eF": do-lowercase-version
"\eG": do-lowercase-version
"\eH": do-lowercase-version
"\eI": do-lowercase-version
"\eJ": do-lowercase-version
"\eK": do-lowercase-version
"\eL": do-lowercase-version
"\eM": do-lowercase-version
"\eN": do-lowercase-version
"\eOE": do-lowercase-version
"\eOF": do-lowercase-version
"\eOG": do-lowercase-version
"\eOH": do-lowercase-version
"\eOI": do-lowercase-version
"\eOJ": do-lowercase-version
"\eOK": do-lowercase-version
"\eOL": do-lowercase-version
"\eOM": do-lowercase-version
"\eON": do-lowercase-version
"\eOO": do-lowercase-version
"\eOP": do-lowercase-version
"\eOQ": do-lowercase-version
"\eOR": do-lowercase-version
"\eOS": do-lowercase-version
"\eOT": do-lowercase-version
"\eOU": do-lowercase-version
"\eOV": do-lowercase-version
"\eOW": do-lowercase-version
"\eOX": do-lowercase-version
"\eOY": do-lowercase-version
"\eOZ": do-lowercase-version
"\eP": do-lowercase-version
"\eQ": do-lowercase-version
"\eR": do-lowercase-version
"\eS": do-lowercase-version
"\eT": do-lowercase-version
"\eU": do-lowercase-version
"\eV": do-lowercase-version
"\eW": do-lowercase-version
"\eX": do-lowercase-version
"\eY": do-lowercase-version
"\eZ": do-lowercase-version
"\e[1A": do-lowercase-version
"\e[1B": do-lowercase-version
"\e[1C": do-lowercase-version
"\e[1D": do-lowercase-version
"\e[1E": do-lowercase-version
"\e[1F": do-lowercase-version
"\e[1G": do-lowercase-version
"\e[1H": do-lowercase-version
"\e[1I": do-lowercase-version
"\e[1J": do-lowercase-version
"\e[1K": do-lowercase-version
"\e[1L": do-lowercase-version
"\e[1M": do-lowercase-version
"\e[1N": do-lowercase-version
"\e[1O": do-lowercase-version
"\e[1P": do-lowercase-version
"\e[1Q": do-lowercase-version
"\e[1R": do-lowercase-version
"\e[1S": do-lowercase-version
"\e[1T": do-lowercase-version
"\e[1U": do-lowercase-version
"\e[1V": do-lowercase-version
"\e[1W": do-lowercase-version
"\e[1X": do-lowercase-version
"\e[1Y": do-lowercase-version
"\e[1Z": do-lowercase-version
"\e[4A": do-lowercase-version
"\e[4B": do-lowercase-version
"\e[4C": do-lowercase-version
"\e[4D": do-lowercase-version
"\e[4E": do-lowercase-version
"\e[4F": do-lowercase-version
"\e[4G": do-lowercase-version
"\e[4H": do-lowercase-version
"\e[4I": do-lowercase-version
"\e[4J": do-lowercase-version
"\e[4K": do-lowercase-version
"\e[4L": do-lowercase-version
"\e[4M": do-lowercase-version
"\e[4N": do-lowercase-version
"\e[4O": do-lowercase-version
"\e[4P": do-lowercase-version
"\e[4Q": do-lowercase-version
"\e[4R": do-lowercase-version
"\e[4S": do-lowercase-version
"\e[4T": do-lowercase-version
"\e[4U": do-lowercase-version
"\e[4V": do-lowercase-version
"\e[4W": do-lowercase-version
"\e[4X": do-lowercase-version
"\e[4Y": do-lowercase-version
"\e[4Z": do-lowercase-version
"\e[E": do-lowercase-version
"\e[F": do-lowercase-version
"\e[G": do-lowercase-version
"\e[H": do-lowercase-version
"\e[I": do-lowercase-version
"\e[J": do-lowercase-version
"\e[K": do-lowercase-version
"\e[L": do-lowercase-version
"\e[M": do-lowercase-version
"\e[N": do-lowercase-version
"\e[O": do-lowercase-version
"\e[P": do-lowercase-version
"\e[Q": do-lowercase-version
"\e[R": do-lowercase-version
"\e[S": do-lowercase-version
"\e[T": do-lowercase-version
"\e[U": do-lowercase-version
"\e[V": do-lowercase-version
"\e[W": do-lowercase-version
"\e[X": do-lowercase-version
"\e[Y": do-lowercase-version
"\e[Z": do-lowercase-version
"\el": downcase-word
# dump-functions (not bound)
# dump-macros (not bound)
# dump-variables (not bound)
"\e\C-i": dynamic-complete-history
# emacs-editing-mode (not bound)
"\C-x)": end-kbd-macro
"\e>": end-of-history
"\C-e": end-of-line
"\e[4~": end-of-line
"\C-x\C-x": exchange-point-and-mark
# forward-backward-delete-char (not bound)
"\C-f": forward-char
"\eOC": forward-char
"\e[C": forward-char
"\C-s": forward-search-history
"\ef": forward-word
"\C-x*": glob-expand-word
"\C-xg": glob-list-expansions
# history-and-alias-expand-line (not bound)
"\e^": history-expand-line
# history-search-backward (not bound)
# history-search-forward (not bound)
"\e#": insert-comment
"\e*": insert-completions
"\e.": insert-last-argument
"\e_": insert-last-argument
"\C-k": kill-line
# kill-region (not bound)
# kill-whole-line (not bound)
"\ed": kill-word
# magic-space (not bound)
# menu-complete (not bound)
"\C-n": next-history
"\eOB": next-history
"\e[B": next-history
"\en": non-incremental-forward-search-history
# non-incremental-forward-search-history-again (not bound)
"\ep": non-incremental-reverse-search-history
# non-incremental-reverse-search-history-again (not bound)
"\C-o": operate-and-get-next
# paste-from-clipboard (not bound)
"\C-x!": possible-command-completions
"\e=": possible-completions
"\e?": possible-completions
"\C-x/": possible-filename-completions
"\C-x@": possible-hostname-completions
"\C-x~": possible-username-completions
"\C-x$": possible-variable-completions
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
"\C-q": quoted-insert
"\C-v": quoted-insert
"\C-x\C-r": re-read-init-file
# redraw-current-line (not bound)
"\C-r": reverse-search-history
"\e\C-r": revert-line
"\er": revert-line
" ": self-insert
"!": self-insert
"\"": self-insert
"#": self-insert
"$": self-insert
"%": self-insert
"&": self-insert
"'": self-insert
"(": self-insert
")": self-insert
"*": self-insert
"+": self-insert
",": self-insert
"-": self-insert
".": self-insert
"/": self-insert
"0": self-insert
"1": self-insert
"2": self-insert
"3": self-insert
"4": self-insert
"5": self-insert
"6": self-insert
"7": self-insert
"8": self-insert
"9": self-insert
":": self-insert
";": self-insert
"<": self-insert
"=": self-insert
">": self-insert
"?": self-insert
"@": self-insert
"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert
"K": self-insert
"L": self-insert
"M": self-insert
"N": self-insert
"O": self-insert
"P": self-insert
"Q": self-insert
"R": self-insert
"S": self-insert
"T": self-insert
"U": self-insert
"V": self-insert
"W": self-insert
"X": self-insert
"Y": self-insert
"Z": self-insert
"[": self-insert
"\\": self-insert
"]": self-insert
"^": self-insert
"_": self-insert
"`": self-insert
"a": self-insert
"b": self-insert
"c": self-insert
"d": self-insert
"e": self-insert
"f": self-insert
"g": self-insert
"h": self-insert
"i": self-insert
"j": self-insert
"k": self-insert
"l": self-insert
"m": self-insert
"n": self-insert
"o": self-insert
"p": self-insert
"q": self-insert
"r": self-insert
"s": self-insert
"t": self-insert
"u": self-insert
"v": self-insert
"w": self-insert
"x": self-insert
"y": self-insert
"z": self-insert
"{": self-insert
"|": self-insert
"}": self-insert
"~": self-insert
"\200": self-insert
"\201": self-insert
"\202": self-insert
"\203": self-insert
"\204": self-insert
"\205": self-insert
"\206": self-insert
"\207": self-insert
"\210": self-insert
"\211": self-insert
"\212": self-insert
"\213": self-insert
"\214": self-insert
"\215": self-insert
"\216": self-insert
"\217": self-insert
"\220": self-insert
"\221": self-insert
"\222": self-insert
"\223": self-insert
"\224": self-insert
"\225": self-insert
"\226": self-insert
"\227": self-insert
"\230": self-insert
"\231": self-insert
"\232": self-insert
"\233": self-insert
"\234": self-insert
"\235": self-insert
"\236": self-insert
"\237": self-insert
" ": self-insert
"¡": self-insert
"¢": self-insert
"£": self-insert
"¤": self-insert
"¥": self-insert
"¦": self-insert
"§": self-insert
"¨": self-insert
"©": self-insert
"ª": self-insert
""": self-insert
"¬": self-insert
"­": self-insert
"®": self-insert
"¯": self-insert
"°": self-insert
"±": self-insert
"²": self-insert
"³": self-insert
"´": self-insert
"µ": self-insert
"¶": self-insert
"·": self-insert
"¸": self-insert
"¹": self-insert
"º": self-insert
""": self-insert
"¼": self-insert
"½": self-insert
"¾": self-insert
"¿": self-insert
"À": self-insert
"Á": self-insert
"Â": self-insert
"Ã": self-insert
"Ä": self-insert
"Å": self-insert
"Æ": self-insert
"Ç": self-insert
"È": self-insert
"É": self-insert
"Ê": self-insert
"Ë": self-insert
"Ì": self-insert
"Í": self-insert
"Î": self-insert
"Ï": self-insert
"Ð": self-insert
"Ñ": self-insert
"Ò": self-insert
"Ó": self-insert
"Ô": self-insert
"Õ": self-insert
"Ö": self-insert
"×": self-insert
"Ø": self-insert
"Ù": self-insert
"Ú": self-insert
#"Û": self-insert
"Ü": self-insert
"Ý": self-insert
"Þ": self-insert
"ß": self-insert
"à": self-insert
"á": self-insert
"â": self-insert
"ã": self-insert
"ä": self-insert
"å": self-insert
"æ": self-insert
"ç": self-insert
"è": self-insert
"é": self-insert
"ê": self-insert
"ë": self-insert
"ì": self-insert
"í": self-insert
"î": self-insert
"ï": self-insert
"ð": self-insert
"ñ": self-insert
"ò": self-insert
"ó": self-insert
"ô": self-insert
"õ": self-insert
"ö": self-insert
"÷": self-insert
"ø": self-insert
"ù": self-insert
"ú": self-insert
"û": self-insert
"ü": self-insert
"ý": self-insert
"þ": self-insert
"ÿ": self-insert
"\C-@": set-mark
"\e ": set-mark
"\e\C-e": shell-expand-line
"\C-x(": start-kbd-macro
# tab-insert (not bound)
"\e&": tilde-expand
"\C-t": transpose-chars
"\et": transpose-words
# tty-status (not bound)
"\C-x\C-u": undo
"\C-_": undo
# universal-argument (not bound)
"\C-u": unix-line-discard
"\C-w": unix-word-rubout
"\eu": upcase-word
# vi-append-eol (not bound)
# vi-append-mode (not bound)
# vi-arg-digit (not bound)
# vi-bWord (not bound)
# vi-back-to-indent (not bound)
# vi-bracktype (not bound)
# vi-bword (not bound)
# vi-change-case (not bound)
# vi-change-char (not bound)
# vi-change-to (not bound)
# vi-char-search (not bound)
# vi-column (not bound)
# vi-complete (not bound)
# vi-delete (not bound)
# vi-delete-to (not bound)
# vi-eWord (not bound)
# vi-editing-mode (not bound)
# vi-end-word (not bound)
# vi-eof-maybe (not bound)
# vi-eword (not bound)
# vi-fWord (not bound)
# vi-fetch-history (not bound)
# vi-first-print (not bound)
# vi-fword (not bound)
# vi-goto-mark (not bound)
# vi-insert-beg (not bound)
# vi-insertion-mode (not bound)
# vi-match (not bound)
# vi-movement-mode (not bound)
# vi-next-word (not bound)
# vi-overstrike (not bound)
# vi-overstrike-delete (not bound)
# vi-prev-word (not bound)
# vi-put (not bound)
# vi-redo (not bound)
# vi-replace (not bound)
# vi-search (not bound)
# vi-search-again (not bound)
# vi-set-mark (not bound)
# vi-subst (not bound)
# vi-tilde-expand (not bound)
# vi-yank-arg (not bound)
# vi-yank-to (not bound)
"\C-y": yank
"\e.": yank-last-arg
"\e_": yank-last-arg
"\e\C-y": yank-nth-arg
"\ey": yank-pop

Tip inputrc settings

Hello

put the following either in ~/.inputrc or /etc/inputrc if you find yourself
using cli from time to time and want to make it a bit easier:

# do not casesensitive completion
set completion-ignore-case on

# if there are more than 150 possible completions for a word, ask the
# user if he wants to see all of them
set completion-query-items 150

# Adding this to your /etc/inputrc or ~/.inputrc will result in a character
# being appended to any file-names returned by completion, in much the same
# way as ls -F works.
set visible-stats on

# If you have this in your /etc/inputrc or ~/.inputrc, you will no longer
# have to hit the  key twice to produce a list of all possible
# completions.
# A single   will suffice.
set show-all-if-ambiguous on

# Completed names which are symbolic links to
# directories have a slash appended.
set mark-symlinked-directories on
This one is already in /etc/inputrc and only needs to be "uncommented":
# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
Have fun

bye Thilo
--
i am on Ubuntu 2.6 KDE
- some friend of mine

gpg key: 0x4A411E09

Inputrc for bash history completion using up/down arrows

The bashrc file stores key mappings. Use your own bashrc by putting export INPUTRC=~/.inputrc in your .bash_profile or .bashrc

This page, Creating the /etc/inputrc File and this page, Super-useful inputrc give some useful advice about things you can put in those files.

One of things I most often find myself doing is searching my command line history. I frequently use the cursor up and down to scroll through my most recent commands. Often, I want to re-use a particular grep or find that I used recently, but I don't remember the specifics of it. With the text below in your .inputrc, you can type in the first few letters, say gr or f and press the cursor keys and it will scroll through your command history, showing the commands that began with those characters. Nice. :)

  1. # By default up/down are bound to previous-history
  2. # and next-history respectively. The following does the
  3. # same but gives the extra functionality where if you
  4. # type any text (or more accurately, if there is any text
  5. # between the start of the line and the cursor),
  6. # the subset of the history starting with that text
  7. # is searched (like 4dos for e.g.).
  8. # Note to get rid of a line just Ctrl-C
  9. "\e[B": history-search-forward
  10. "\e[A": history-search-backward
  11. $if Bash
  12. # F10 toggles mc on and off
  13. # Note Ctrl-o toggles panes on and off in mc
  14. "\e[21~": "mc\C-M"
  15. #do history expansion when space entered
  16. Space: magic-space
  17. $endif
  18. # Include system wide settings which are ignored
  19. # by default if one has their own .inputrc
  20. $include /etc/inputrc

Super-useful inputrc

Linuxart

You either want to place the following in ~/.inputrc or /etc/inputrc… I've found that while ~/.inputrc works sometimes, it doesn't on all systems.


"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
set completion-ignore-case On

All lines except the last enable nice readline & bash cursor movement (control + arrow keys and what not) while the last line turns on case insensitivity for tab-completion, enabling you to have folders and files of mixed case characters while not having to type the capital letters. (You can have a directory called "Documents" and tab-complete by tying "doc<tab>"

I've been enjoying the above for a while now, and I think it really should be the default settings for distributions.

Note: This works for Linux, Mac OS X, and *BSD. It might work for people using Bash on Windows, but you're on your own there. (:

No TweetBacks yet. (Be the first to Tweet this post)

This entry was posted on Thursday, October 13th, 2005 at 1:23 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  1. Jürg Billeter October 13th, 2005 at 2:08 pm

    With one change that's a very nice default configuration.

    "\e[5~": history-search-backward
    "\e[6~": history-search-forward

    This maps Page Up / Page Down to the IMO very useful history search function. Configured like that by default in paldo.

  2. Marius Gedminas October 13th, 2005 at 3:53 pm

    If ~/.inputrc doesn't work, then you need to add

    export INPUTRC=~/.inputrc

    to your .bashrc/.bash_profile/wherever.

  3. Colin Marquardt October 13th, 2005 at 5:28 pm

    These should all be "quote-backslash-e…", right? The backslash is missing for me.

    I have a few more variables set:

    set bell-style visible
    set expand-tilde on
    set convert-meta off
    set input-meta on
    set output-meta on
    set show-all-if-ambiguous on
    set visible-stats on

  4. Pádraig Brady October 14th, 2005 at 5:36 am

    I acutally go the whole hog and do like 4dos used to.
    I.E. have the up/down arrows do the search.
    Note if you don't type anything the up/down arrow behave as before.
    http://www.pixelbeat.org/settings/.inputrc

  5. Wouter October 14th, 2005 at 9:13 am

    Yes you need to escape with a \\ before the first e. Doesn't seem to work in my xterm (debian) though.. :(

  6. Anonymous October 14th, 2005 at 1:50 pm

    Works great in gnome-terminal; thanks!

    I also tried to get Control-Backspace to do unix-filename-rubout, but unfortunately it seems that Control-Backspace doesn't look any differently from Backspace.

  7. Francesco January 15th, 2006 at 9:56 am

    for those who like the WinNT/XP way TAB works in MSDOS prompts (i.e. instead of showing a list of possible completions like bash does on default, TAB cycles through possible completions), just add this to .inputrc:

    "\t": menu-complete

    it saves me a lot of typing !!

  8. Jason May 1st, 2006 at 1:15 pm

    Anybody know how to make a shortcut so that way when i press "ctrl+a" or whatever letter, i can set it to do an action…i was told it was with the /etc/inputrc file, but i can't figure it out.

  9. ameet June 19th, 2006 at 4:07 am

    how to make readline auto compleate search the words to be compleated from the file.

  10. Triffid Hunter July 6th, 2006 at 10:36 pm

    I had to add:
    "\e[1;5C": forward-word
    "\e[1;5D": backward-word
    on my (gentoo linux) system for word search to work. A similar translation should apply nicely to the others.

  11. micha September 2nd, 2006 at 8:31 pm

    I noticed what appears to be a bug in readline. I'm using Terminal.app on tiger with bash, and doing 'set completion-ignore-case On' causes completion to break with directories that have spaces in the name. For example:

    /home/micha/Documents/My Stuff/myfile.pdf

    If, from /home/micha I do

    ls documents/my\ [TAB]

    it completes correctly to produce Documents/My\ Stuff/ but if I press tab again at that point it produces this:

    ls Documents/My\\\ Stuff

    which is wrong.

  12. nix December 25th, 2006 at 12:44 am

    One simple question:

    How does the keyseq get generated? Are they predefined values? Do they relate to the ansi key set?

    IE: "\e[3~": delete-char
    How does the value: "\e[3~": relate to the delete key?

  13. Will February 5th, 2007 at 7:00 pm

    Q: How does the value: "\e[3~": relate to the delete key?

    A: "\e[3~" is the sequence of bytes that your terminal sends when the delete key is pressed. And when I say "terminal", I mean either a real serial terminal, or an xterm-like thing, or the Linux console driver.

  14. kortina July 5th, 2007 at 3:02 pm

    Does this work with iterm in os x? I copied and pasted

    "\e[1~": beginning-of-line
    "\e[4~": end-of-line
    "\e[5~": beginning-of-history
    "\e[6~": end-of-history
    "\e[3~": delete-char
    "\e[2~": quoted-insert
    "\e[5C": forward-word
    "\e[5D": backward-word
    "\e\e[C": forward-word
    "\e\e[D": backward-word

    into my ~/.inputrc and tried doing export INPUTRC=~/.inputrc but forward and backword word do not work for me. Anyone got any suggestions?

  15. omega January 3rd, 2008 at 7:23 pm

    kortina: if you typed "export INPUTRC=~/.inputrc" into your terminal that just sets the variable, doesn't actually read the file. you either have to type it in your ~/.bash_profile or just put those settings in /etc/inputrc instead of locally.

    oh, i'm not sure about if it works with iterm, i just use the Terminal in leopard (works just fine for me, i've yet to find something that iterm does and Terminal doesn't - besides fullscreening which i don't like anyway)

    but yes, it does work with mac os x :)

  16. Rich February 28th, 2008 at 4:43 pm

    Whoops. Looks like somebody's blog is converting double quote characters into posh quotes. Notice how there's a difference between "these quotes" and "these"

  17. kwilliam June 9th, 2008 at 3:48 pm

    Thank you blog author and Marius Gedminas!
    After much fruitless searching, I have finally gotten the console to use a visible bell instead of beeping loudly.

    I added line
    set bell-style visible
    to ~/.inputrc
    and added line
    export INPUTRC=~/.inputrc
    to ~/.bashrc

  18. Binny V A February 7th, 2009 at 7:57 am

    Thanks for this post. If possible, can you and some comments to show which keys you are using? I know [5~, [6~ is Page up/Page down - but the rest have me a bit confused.

  19. andre February 26th, 2009 at 9:08 am

    Debian has a file explaining this:

    /usr/share/doc/bash/inputrc.arrows:
    # This file controls the behaviour of line input editing for
    # programs that use the Gnu Readline library.
    #
    # Arrow keys in keypad mode
    #
    "\C-[OD" backward-char
    "\C-[OC" forward-char
    "\C-[OA" previous-history
    "\C-[OB" next-history
    #
    # Arrow keys in ANSI mode
    #
    "\C-[[D" backward-char
    "\C-[[C" forward-char
    "\C-[[A" previous-history
    "\C-[[B" next-history
    #
    # Arrow keys in 8 bit keypad mode
    #
    "\C-M-OD" backward-char
    "\C-M-OC" forward-char
    "\C-M-OA" previous-history
    "\C-M-OB" next-history
    #
    # Arrow keys in 8 bit ANSI mode
    #
    "\C-M-[D" backward-char
    "\C-M-[C" forward-char
    "\C-M-[A" previous-history
    "\C-M-[B" next-history

Readline

Bash Reference Manual Readline Init File

Although the Readline library comes with a set of Emacs-like keybindings installed by default, it is possible to use a different set of keybindings. Any user can customize programs that use Readline by putting commands in an inputrc file, conventionally in his home directory. The name of this file is taken from the value of the shell variable INPUTRC. If that variable is unset, the default is `~/.inputrc'.

When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.

In addition, the C-x C-r command re-reads this init file, thus incorporating any changes that you might have made to it.

8.3.1 Readline Init File Syntax Syntax for the commands in the inputrc file.
8.3.2 Conditional Init Constructs Conditional key bindings in the inputrc file.

8.3.3 Sample Init File An example inputrc file.

etc/inputrc

etc/inputrc deals with the mapping of the keyboard for certain situations. This file is the start-up file used by readline - the input related library used by Bash and most other shells.

For more information see info bash -- Node: Readline Init file as well as info readline. There is a lot that can be done with this one rc file.

The following is a base /etc/inputrc along with comments to explain what the various options do.

Please note that comments can not be on the same line as commands in inputrc.

# Begin /etc/inputrc

# Make sure we don't output everything on the 1 line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On 
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the 
# value contained inside the 1st argument to the 
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# End /etc/inputrc


Global values are set in /etc/inputrc. Personal user values as are set in ~/.inputrc. The ~/.inputrc file will override the global settings file. The previous page sets up Bash to use /etc/inputrc by default. If you want your system to use both, it might be a good idea to place a default .inputrc into the /etc/skel directory for use with new users.

The default behavior of the keyboard often leaves a lot to be desired. It is actually readline that handles this. Readline is a separate package that handles command line interfaces, providing the command history and filename completion, as well as some advanced line editing features. It is compiled into bash. By default, readline is configured using the file .inputrc in your home directory. The bash variable INPUTRC can be used to override this for bash.

Once bash has read the system-wide configuration file, it looks for your personal configuration file. It checks in your home directory for .bash_profile, .bash_login and .profile. It runs the first one of these it finds. If you want to change the way bash behaves for you, without changing the way it works for others, do it here. For example, many applications use environment variables to control how they work. I have the variable EDITOR set to vi so that I can use vi in Midnight Commander (an excellent console based file manager) instead of its editor.

Bash Config Files

How Bash executes startup files.

For Login shells (subject to the -noprofile option):

On logging in:
If `/etc/profile' exists, then source it.

If `~/.bash_profile' exists, then source it,
else if `~/.bash_login' exists, then source it,
else if `~/.profile' exists, then source it.

On logging out:
If `~/.bash_logout' exists, source it.

For non-login interactive shells (subject to the -norc and -rcfile options):
On starting up:
If `~/.bashrc' exists, then source it.

For non-interactive shells:
On starting up:
If the environment variable `ENV' is non-null, expand the variable and source the file named by the value. If Bash is not started in Posix mode, it looks for `BASH_ENV' before `ENV'.

So, typically, your `~/.bash_profile' contains the line
`if [ -f `~/.bashrc' ]; then source `~/.bashrc'; fi' after (or before) any login specific initializations.

If Bash is invoked as `sh', it tries to mimic the behavior of `sh' as closely as possible. For a login shell, it attempts to source only `/etc/profile' and `~/.profile', in that order. The `-noprofile' option may still be used to disable this behavior. A shell invoked as `sh' does not attempt to source any other startup files.

When Bash is started in POSIX mode, as with the `-posix' command line option, it follows the Posix 1003.2 standard for startup files. In this mode, the `ENV' variable is expanded and that file sourced; no other startup files are read.

My .bashrc can be found here.

My .bash_profile can be found here.

.inputrc (readline)--

Although the Readline library comes with a set of Emacs-like key bindings installed by default, it is possible that you would like to use a different set of keybindings. You can customize programs that use Readline by putting commands in an "init" file in your home directory. The name of this file is taken from the value of the shell variable `INPUTRC'. If that variable is unset, the default is `~/.inputrc'.

When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.

In addition, the `C-x C-r' command re-reads this init file, thus incorporating any changes that you might have made to it.

Conditional Init Constructs within readline

Readline implements a facility similar in spirit to the conditional compilation features of the C preprocessor which allows key bindings and variable settings to be performed as the result of tests. There are three parser directives used.

`$if' The `$if' construct allows bindings to be made based on the editing mode, the terminal being used, or the application using Readline. The text of the test extends to the end of the line; no characters are required to isolate it.
`mode' The `mode=' form of the `$if' directive is used to test whether Readline is in `emacs' or `vi' mode. This may be used in conjunction with the `set keymap' command, for instance, to set bindings in the `emacs-standard' and `emacs-ctlx' keymaps only if Readline is starting out in `emacs' mode.
`term' The `term=' form may be used to include terminal-specific key bindings, perhaps to bind the key sequences output by the terminal's function keys. The word on the right side of the `=' is tested against the full name of the terminal and the portion of the terminal name before the first `-'. This allows SUN to match both SUN and SUN-CMD, for instance.
`application' The APPLICATION construct is used to include application-specific settings. Each program using the Readline library sets the APPLICATION NAME, and you can test for it. This could be used to bind key sequences to
functions useful for a specific program.
`$endif' This command terminates an `$if' command.
`$else' Commands in this branch of the `$if' directive are executed if the test fails.

The following command adds a key sequence that quotes the current or previous word in Bash:
$if bash
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
$endif

My .inputrc file is here

Last update by Hermann Heimhardt on October 7, 2001

BASH inputrc Function Binding to Key Ignored


From: William Bloom
Subject: BASH inputrc Function Binding to Key Ignored
Date: Thu, 7 Jun 2001 21:56:02 -0700 (MST)

I have built BASH 2.05.0(1)-release from source for Solaris
8 as well as FreeBSD 4.3 and have observed a difference from
BASH 2.04 in the handling of the TAB keybinding when 'vi'
editing mode is in use with a POSIX mode BASH session.
I've not seen comments about this in the archive, and the
new behavior continues even after I've applied all patches
from the BASH homepage.  I also don't see comments in the
changelog that seem relevant (perhaps I've missed it).

As an example, suppose  I have a very simple inputrc file as
follows...

        TAB: complete
        C-b: complete-into-braces

...as part of a profile that contains a 'set -o vi'.  The
login shell is /usr/local/bin/sh (a link to BASH) in order
to have a POSIX session.  The above inputrc is simply
intended to let TAB have the same completion behavior to
which some of my users are accustomed from non-POSIX BASH
use.

I find that in this case, the 'C-b' binding is honored but
the TAB binding is ignored.  -After- the session is started,
the TAB binding may be enabled using 'bind -f .inputrc', at
which time the TAB binding is not ignored.

Stranger yet, if the account is changed to use
/usr/local/bin/bash as the login shell, then the session
login now honors the TAB binding.  If the session switches
to POSIX mode (set -o posix) after login, however, then the
TAB binding automagically disappears (although the C-b binding
remains).

Is this behavior related to POSIX compliance, which (I
believe) does not call for TAB to be bound?  Are those users
who want POSIX mode and vi-style command line editing, but
yet aren't ready to give up their accustomed TAB-complete
binding, going to have to have an explicit...

        bind 'TAB: complete'

...to their login profile as of 2.0.5?


Bill

Lee D. Rothstein - Re remapping Cygwin 'bash' readline functions to PC keys

John, thanks for the <C-V> heads up! Others had
suggested, variations of 'cat < foo' and 'od -c'. (The
former I got to work, the latter remains a mystery.)
Your solution, besides being the most straight-forward,
is also a great tool to have around. Apparently, it's a
feature of Cygwin, or 'bash', since it doesn't work in
a naked 'cmd.exe' window. I'll be sure to add it to my
documentation.
 
Unfortunately the key combos I'm trying to map to
are: <^-->> and <^-<-> (control- and the right and left
arrow keys). It turns out that the character string
outputs for both the:
 
* naked key
* shift - and the naked key
* control- and the naked key


are all the same.

Actually, I no longer consider these to be KIDs; these
are the character string graphemic outputs of hitting
the key. I'll continue to reserve "KIDs" for when one
represents these [and the key isomorphisms]
with '/e...', 'C-...', etc. "notation".
 
Apparently (?), the only way to discriminate among these
three alternatives is with Scan Codes. Apparently, all
the -x-ish stuff I've used (Microemacs, Thompson shell
command line editing) that can discriminate among the
three alternatives all use Scan Codes (?).
 
 - Any way to map to Scan Codes to 'bash' 'readline'
   functions under Cygwin?
   + Or to key "names" like: '<CTRL-left-arrow>',
     '<CTRL-HOME>'
 - Any interest among Cygwin developers in adding
   this?
 
>At 2003-02-19 08:02 AM -0800, John Mapole wrote:
...
>You can build your own KID table. Once at the cygwin
>prompt you can type <C-V>, that's control-V, followed
>by the key.  On my machine, if I type <C-V><INSERT>, I
>see "^[[2~".  This is the same as "\e[2~".
>
>Why these mapping are like this relates to how windows
>maps them and then how cygwin maps them.  Something I
>am now very clear on.
>
>Hope this helps some.
>
>John Mapoles
>
>--- "Lee D. Rothstein" <lee at veritech dot com> wrote:
...
>> Q1 -- When you remap a 'bash' Edit Mode function in
>> .inputrc, it looks like this:
>>
>> "\e[3~":      delete-char # DEL key
>>
>> The entity in double quotes ("\e[3~"), I'm calling
>> the "key ID (KID)". In the above '.inputrc' declaration,
>> the function 'delete-char' being remapped from its
>> default key assignment to the KID -- "\e[3~" -- the
>> <DEL> key.
>>
>> What are the KIDs of the following IBM PC keys
>> (specified below with facsimiles of the key caps
>> contained in angle brackets -- '<...>')?
>>
>> Cursor control key pad
>> ----------------------
>> <HOME>
>> <END>
>> <left-arrow>
>> <right-arrow>
>> <PAGE_UP>
>> <PAGE_DOWN>
>> <INSERT>
>>
>> Numeric pad
>> -----------
>> <->
>> <+>
>> <ENTER>
>> </>


I should have included in the above lists, all variations
of the above with the control, alt and shift keys.


>> In general, I'd like a table that maps the KIDs for all
>> 104 keys on the keyboard I use. Or, better still, is
>> there a way to use scan codes?
>>
>> (Incidentally, what makes finding a table of these
>> KIDs so difficult is the failure of the documentation
>> to assign this concept a unique, or even a consistent
>> word.)
>>
>> Q2
>> --
>>
>> Is there a way to make the <INSERT> key a toggle
>> between the insert and overwrite modes of 'bash'
>> edit mode?
>>
>> I used to have these figured out for 'Microemacs',
>> but that was half a lifetime ago, for me, & Microemacs
>> supported scan codes, if I remember correctly.


--
Lee D. Rothstein -- lee at veritech dot com
VeriTech -- 603-424-2900
7 Merry Meeting Drive
Merrimack, NH 03054-2934

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Why can't solaris be more like linux...

Matthew Frederico [email protected]
16 May 2003 12:53:12 -0600

I agree with Nick on this one. I don't know about the inputrc stuff but
I do know that in the past, I would set my TERM to VT220 then all my
keys worked correctly.  Try:
% export TERM=VT220


On Fri, 2003-05-16 at 12:23, Nicholas Leippe wrote:
> On Friday 16 May 2003 10:14 am, you wrote:
> > I've been given access to a bunch of solaris 8 boxen here at work
> > lately, and i'm finding it difficult to move around in it because of
> > shell differences... i guess i've always taken the default setups of any
> > linux distro i've ever tried for granted.  On the solaris box, I've had
> > my shell set to BASH as that is my preference... but i still get "^?"
> > characters while trying to backspace in vi and from ftp... and i get "~"
> > when trying to "delete".  Anyone know of a quick way to get the shell
> > setup "correctly"?
> 
> Trying creating a ~/.inputrc file (see READLINE section in the bash man page).
> My global inputrc in linux (/etc/inputrc) is:
> 
> set meta-flag on
> set input-meta on
> set convert-meta off
> set output-meta on
> "\e0d": backward-word
> "\e0c": forward-word
> "\e[h": beginning-of-line
> "\e[f": end-of-line
> "\e[1~": beginning-of-line
> "\e[4~": end-of-line
> "\e[5~": beginning-of-history
> "\e[6~": end-of-history
> "\e[3~": delete-char
> "\e[2~": quoted-insert
> "\e[g": beginning-of-line
> "\e[e": beginning-of-line
> "\e[H": beginning-of-line
> "\e[F": end-of-line
> "\e[G": beginning-of-line
> "\e[E": beginning-of-line
> "\e[s": beginning-of-line
> DEL: backward-delete-char
> 
> 
> Some of that came from Redhat 6.2's /etc/inputrc.  You could also try copying 
> it from whatever distro you have installed nearby.  Also, check your TERM 
> variable.  When that's not correct, some things can get really wierd. (eg it 
> shouldn't be 'xterm' when in the console)
> 
> 
> Nick

-- 
Matthew Frederico
Unwired Networks
[email protected]
http://www.unwirednetworks.com

Re [ale] Automating vi-style history editing in bash


On Wed, Mar 01, 2000 at 04:55:31PM -0500, David S. Jackson wrote:
> On Wed, Mar 01, 2000 at 03:09:22PM -0500 Fulton Green <[email protected]> wrote:
> > I know how to set up bash to let me edit previous commands vi-style by typing
> > set -o vi
> > or
> > export EDITOR=vi
>
> These two things are not really the same at all. Your shell's
> editing mode (if it is bash) is set with set -o vi or set -o
> emacs. Or you can emulate yet another editor by setting the
> $readline variable in .inputrc.
>
> However, the $EDITOR variable is something else. Applications
> check if this variable exists if no editor is defined by default
> for that application. You can set emacs editing mode for the
> shell and set the $EDITOR variable to vi. No problem.

True for bash, and you can do this in ksh as well. However, ksh also examines
$EDITOR if the readline mode hasn't been set with 'set -o vi|emacs'. At least
this is the behavior on Solaris 2.6, whose Korn shell doesn't appear to do
the inputrc thing. My apologies for confusing Korn's behavior with bash's.

> > at the command line. My problem: under Red Hat 6.1, I can't seem to get
> > either of these to have an effect when I put either of them in .bash_profile
> > or .bashrc
>
> Again, the statements don't mean the same thing. But just open
> an Xterm (or whatever) and type set -o vi at the command prompt.

And that works for me.

> > In fact, doing the "set" command in .bashrc ensures that I won't
> > even be able to *manually* set the mode from the command line.
>
> This shouldn't be true. Using the set command shouldn't prohibit
> you from 'unsetting' the editing mode. I normally use emacs mode

It *is* true in my circumstance, but it's iff the 'set -o vi' is executed in
my .bashrc file. And at this point, I should mention that I didn't notice any
problem until I upgraded Red Hat to 6.0. I also confirmed the problem on a
fresh RH 6.1 box. I'm going to change the main runlevel in /etc/inittab from
5 to 3 (i.e., don't start out in an X server) to see if that affects anything.

> Are you using an .inputrc?

I'm not. That could very well be the problem if the global inputrc has
trash, as pointed out by earlier emails. I don't have the computer in question
in front of me at the moment, but I could almost swear that RH 6.0 or 6.1 did
away with /etc/inputrc . Or was that /etc/skel/.inputrc ?

Thanks for the help,

Fulton Green
http://www.FultonGreen.com/

Recommended Links



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: July 28, 2019