Bash Keyboard Shortcuts – Programming Function Keys

I often backup a file by adding time stamp to the file using command like the one below:

mv catalina.out catalina.out.`date +%F-%I%M%p`

Date command “date +%F-%I%M%p” generates time stamp in the format YYYY-MM-DD-HHMM(AM/PM) (e.g. 2011-01-06-0729PM). Instead of typing the full date command, function keys F1, F2, etc. (not sure what other keys can be programmed) can be set to print it on command line when pressed. This post describes how to program F2 key so that when pressed:

  • deletes a character (backspace)
    • Tab completion adds a space after the filename. This is to delete the space character.
  • prints .`date +%F-%I%M%p` on the command line
Bash Keyboard Shortcut
Before Pressing F2
Bash Keyboard Shortcut
After Pressing F2

Steps:

  1. Open file “.inputrc” in user home directory (vi ~/.inputrc)
  2. Type the following characters (without + sign and spaces):  ” + ctrl-v + F2″
    • double quote
    • while holding the control key press v (similar to holding shift key to type letters in caps)
    • press F2
    • double quote
  3. Type colon
  4. Type the following characters (without + sign and spaces): ” + ctrl-v + ctrl-h + .`date +%F-%I%M%p` + ”
    • double quote
    • while holding the control key press v
    • while holding the control key press h — ctrl-h is for deleting a character (ctrl-h works at command prompt also)
    • .`date +%F-%I%M%p`
    • double quote

The lines should now appear as in the picture below:

Bash Keyboard Shortcut
Entry in .inputrc

Open a new bash shell and pressing F2 should now output .`date +%F-%I%M%p`

Advertisements

One thought on “Bash Keyboard Shortcuts – Programming Function Keys

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s