We all know how to use <ctrl>-R to reverse search through history, but did you know you can use <ctrl>-S to forward search if you set stty stop ""? Also, have you ever tried running bind -p to see all of your keyboard shortcuts listed? There are over 455 on Mac OS X by default.

What is your single most favorite obscure trick, keyboard shortcut or shopt configuration using bash?

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

@andre-r: !!:gs/ehco/echo/ performs a global search and replace on the last command (confer !! in stackoverflow.com/questions/68372/…). That is not equivalent to ^ehco^echo which just replaces one instance of "ehco" -- a more accurate response would be !!:s/ehco/scho.
– Iceland_jackJul 30 '10 at 19:29

I'm a fan of the !$, !^ and !* expandos, returning, from the most recent submitted command line: the last item, first non-command item, and all non-command items. To wit (Note that the shell prints out the command first):

Excellent! All these years the best I could come up with was alias lsd='ls -F | grep --color /', which would list the same thing but in a more lame fashion. However, it would list one dir per line, for ease of parsing. I've modified your command to do the same: ls -d1 */
– Artem RussakovskiiAug 4 '09 at 15:06

how does this work? "ls -d" lists just . (which is weird) but "ls -d */" works.
– YooSep 13 '09 at 16:58

1

It is a bit tricky... "ls -d" is similar to "ls -d ./" and "ls -d /" to "ls -d ./*/". The '-d' switch set 'ls' tu list only directory entries, but if you give it no parameter, it use the current directory as a parameter, so it has *only the "." directory to list...
– edomaurSep 14 '09 at 22:54

As another example, let's say I want to check if two servers have the same list of RPMs installed. Rather than sshing to each server, writing each list of RPMs to separate files, and doing a diff on those files, I can just do the diff from my workstation:

^R reverse search. Hit ^R, type a fragment of a previous command you want to match, and hit ^R until you find the one you want. Then I don't have to remember recently used commands that are still in my history. Not exclusively bash, but also: ^E for end of line, ^A for beginning of line, ^U and ^K to delete before and after the cursor, respectively.

This works the same as ^R but using the arrow keys instead. This means I can type (e.g.) cd /media/ then hit up-arrow to go to the last thing I cd'd to inside the /media/ folder.

(I use Gnome Terminal, you may need to change the escape codes for other terminal emulators.)

Bash completion is also incredibly useful, but it's a far more subtle addition. In ~/.bashrc:

if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi

This will enable per-program tab-completion (e.g. attempting tab completion when the command line starts with evince will only show files that evince can open, and it will also tab-complete command line options).

Works nicely with this also in ~/.inputrc:

set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on

There are thousand of smart tips, cool tricks and efficient recipes that most of the time fit on a single line.

One that I love (but I cheat a bit since I use the fact that Python is installed on most Unix system now) :

alias webshare='python -m SimpleHTTPServer'

Now everytime you type "webshare", the current directory will be available through the port 8000. Really nice when you want to share files with friends on a local network without usb key or remote dir. Streaming video and music will work too.

And of course the classic fork bomb that is completely useless but still a lot of fun :

You can use the watch command in conjunction with another command to look for changes. An example of this was when I was testing my router, and I wanted to get up-to-date numbers on stuff like signal-to-noise ratio, etc.

Why? Because it allows me to look at what's going to be done before I do it. That way if I have a horrible error (like removing my home directory), I can catch it before it happens. Obviously, this is most important for destructive or irrevocable actions.

You might want to use find -print0 and xargs -0 to avoid problems with files and folders with white spaces in them.
– neu242Sep 22 '08 at 8:13

Or avoid creating and working with files and folders with white spaces in their names. ;-) Obviously if you are working with user generated names, your suggestion is vital.
– Jon Ericson♦Sep 22 '08 at 16:09

4

Why would you append "| ksh -s" when you could just remove the "echo"?
– YooSep 13 '09 at 17:27

And then just ctrl+c when I'm done (or if ls returns non-zero). It's similar to the watch program but it uses the shell instead, so it works on platforms without watch.

Another useful tool is netcat, or nc. If you do:

nc -l -p 9100 > printjob.prn

Then you can set up a printer on another computer but instead use the IP address of the computer running netcat. When the print job is sent, it is received by the computer running netcat and dumped into printjob.prn.

Home and End do the same things as crtl-A and ctrl-E.
– davidfg4Apr 15 '09 at 17:01

This helps on Mac Laptops that don't have home and end keys.
– jameshDec 4 '09 at 10:04

Unfortunately ctrl-A is also the escape key for screen by default. I remap it to ctrl-X in my .screenrc, but then I'm not an emacs user.
– system PAUSEDec 11 '09 at 21:44

Somewhat related to the cdable_vars option, there's also the CDPATH environment variable. But that works differently, because you set the parent directory, much like in PATH. With cdable_vars you get more control.
– ZeccNov 17 '10 at 10:45

1

More for your shortcut list: CTRL+K to delete everything from cursor to end of line, CTRL+U to delete everything before cursor, ALT+F/ALT+B to move one word forward/backward (or CTRL+left arrow/CTRL+right arrow). +1 for cdable_vars!
– Philippe A.Jan 30 '12 at 17:39

Using 'set -o vi' from the command line, or better, in .bashrc, puts you in vi editing mode on the command line. You start in 'insert' mode so you can type and backspace as normal, but if you make a 'large' mistake you can hit the esc key and then use 'b' and 'f' to move around as you do in vi. cw to change a word. Particularly useful after you've brought up a history command that you want to change.

Similar to many above, my current favorite is the keystroke [alt]. (Alt and "." keys together) this is the same as $! (Inserts the last argument from the previous command) except that it's immediate and for me easier to type. (Just can't be used in scripts)