Search This Blog

2014-12-03

Advance UNIX: Linux bash Keyboard Shortcut

Product: RedHat, Fedora, CentOS, and various others Linux
Module: bash shell

This post is to demonstrate keyboard shortcut in Linux, which I spent lots of time in. All of these keyboard shortcut keys are in "man bash" page, but even myself don't fully understand it due to it was written for pure UNIX keyboards, instead of Windows keyboard.

I will use PuTTY to demonstrate, and I configured the emulation to xterm, and function keys can be any of those in PuTTY, e.g. ESC[n~, Linux, Xterm R6, VT400, VT100+

$ echo $TERM
xterm

Many people only uses 4 arrow keys to navigate in bash shell. Up to recall last command, down to recall next command, left and right. I won't discuss any of those simple navigation in this post.

This are the files I setup in a directory, and I will use them to illustrate advance editing

$ ls
file10.csv  file15.csv  file28.csv  PhaseInfo14.bz2
file11.csv  file22.csv  file31.csv  PhaseInfo14.csv
file12.csv  file24.csv  org.zip

Tips #1 Auto list multiple files

Shortcut: Alt-Shift-8

I would like to list all the files starts with filename "file."

Step 1: Enter "ll file"

$ ll file1

Step 2: Press Alt-Shift-8, and observe the screen
Step 3: It will automatically fill in all the file name

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv file31.csv

Tips #2 Delete word before cursor

Shortcut: Ctrl-W

While keeping cursor at end of line as shown below

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv file31.csv[cursor here]

Press Ctrl-W, and observe the screen. It will delete "file31.csv"

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv

Tips #3 Move forward/backward a word

Shortcut: Ctrl-F (forward), Ctrl-B (backward)

Note: Not case sentisive

Press these 2 shortcut keys to navigate within the command line. This is much faster than arrow key

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv file31.csv[cursor here]

Press Ctrl-W, and observe the screen. It will delete "file31.csv"

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv

Tips #4 Configure Left/Right Arrow Keys to move by word

Shortcut: Ctrl-Left, Ctrl-Right

Create following text file to configure left/right arrow key behavior. These keys are not configured by default

$ cat > ~/.inputrc << EOF
# Left arrow key - Ctrl-Left
"\eOD": shell-backward-word
# Right arrow key - Ctrl-Right
"\eOC": shell-forward-word
EOF
$ exec bash

The last command is to run bash to activate the new shortcut key (.inputrc)

Try copy-paste following command into your telnet/ssh terminal to test

$ ll file10.csv file11.csv file12.csv file15.csv file22.csv file24.csv file28.csv file31.csv[cursor here]

Press either Ctrl-Left and Ctrl-Right now. You will see the cursor is jumping backward/forward by word

Note: There are Alt-B and Alt-F which can be used for word navigation as well. Ctrl-A and Ctrl-E can be used to go to beginning and end of command line

Tips #5 Deleting word backward

Shortcut: Alt-Backspace, Ctrl-W

Tips #6 Deleting word forward

Shortcut: Alt-D, Ctrl-K

Alt-D can be used to delete word at the cursor location, while Ctrl-K will remove everything after the cursor

Tips #7 Autocomplete Multi-match

Shortcut: Alt-Space

This is again another custom key that I am configuring

$ echo >>   ~/.inputrc << EOF
"\e ": menu-complete
EOF

Let's say I have following files, which I will use to illustrate this shortcut

$ ls
ron000.mp4  ronald.zip  ronChen.txt

Step 1: Type following

$ ls ron

Step 2: Press Alt-Space to auto fill with 1st choice

$ ls ron000.mp4

Step 3: Press Alt-Space to auto fill with 2nd choice

$ ls ronald.zip

Step 4: Press Alt-Space to auto fill with 3rd choice

$ ls ronChen.txt

Step 5: Press Alt-Space and it will ring the "bell" and revert back to original word

$ ls ron

Continue pressing it again, and it will auto fill with additional choice. Then it reach the last one, it will trigger a warning sound in Windows, and not auto fill. If you press further, it will go back to 1st choice

Other Useful Shortcuts

Alt-Shift-` (which is ~) : auto complete username
Alt-Shift-4 (which is $) : auto complete env variable
Alt-Shift-2 (which is @) twice : list all hostname. Expect to see short hostname, FQDN, and localhost
Ctrl-X Ctrl-E : after you obtain the words from autocomplete or UP arrow for previous command, pressing this will allow you to edit the command in vi editor

No comments: