On CBSSports.com: Bargaining in a lousy economy

Find and bind key sequences in bash

Tags: Keyboards, Linux, Vincent Danen, function key, shell, F12, key sequence, Esc, Linux Tips Newsletter

  • Save
  • Print
  • 1

Takeaway: Learn how to use keyboard function keys within bash, or any other shell, by binding the key sequences to a particular shell command.

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.

  • Save
  • Print
  • 1

Print/View all Posts Comments on this article

Assistant ManagerMolly Girl  | 04/27/05
alrightG...  | 05/02/05
Difference in RedHat Linux 9.0Roger  | 05/12/05
Alias listjmgarvin  | 05/12/05

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET
advertisement
Click Here