Find and bind key sequences in bash
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.
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- The Scalable Enterprise: VMware ESX Server on the Dell PowerEdge 6650 Dell This paper introduces the server virtualization software, VMware ESX ... Download Now
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

