So you are happily working in your shell issuing commands with merry abandon. At some point you typing in a long command, but find yourself in the wrong directory. Do you hit ctrl-c, then change directory for then to type in the entire command again?
Enter kill and yank

ctrl-a and ctrl-e

First some basics, ctrl-a will move the cursor to the start of the command line while ctrl-e will move the cursor to the end of the command line.


Kill (ctrl-k) will remove the text after the cursor position and store it in a cut-buffer. If you move to the start of the prompt using ctrl-a, you will the remove the entire line of text, storing that in the cut buffer.


When you need the text again, simply do a yank (ctrl-y) and the shell will copy out the text in the cut buffer into the command line. You can yank repeatedly, and you may access older entries in the yank buffer by pressing ctrl-y esc-y esc-y. (alt-y may work, depending on your keyboard layout).


Using arrows to search through history can get tedious. Thankfully, the bash devs have a solution for that as well. Pressing ctrl-r enters a reverse history search mode. Simply start typing in a search term and the shell will show the first substring match in reverse cronological order. At this point, pressing ctrl-r repeatedly will search further backwards.

Entered a really long one-liner … but realize you wrote “odne” instead of “done” in the middle? ctrl-r can help you with this as well, just hit ctrl-r odne <esc> ctrl-f ctrl-t


Use ctrl-underscore for undo. Unfortunately, it won’t help you recover from horrific errors in your previous command (i.e. rm’ing the wrong directory or shipping bitcoins to the wrong address), but if you find yourself frequently editing complex one-liners and changing your mind, “undo” may prove useful for you.

Alt-. and !$

Insert last argument of previous command. Say you type ls /var/tmp. If you as the next command type cd Alt-., the shell will insert /var/tmp into your commandline, resulting in cd /var/tmp. Repeated Alt-.’s will cycle backward in history.

While !$ is not a keybinding, you end up with the same result. But in this case, the shell will only substitute !$ with the last argument of the previous command only after you have pressed enter:

larso@goethe:~$ mkdir /var/tmp/foo
larso@goethe:~$ cd !$
cd /var/tmp/foo

Other bindings

Many of the tips here are emacs keybindings - most other keybindings that works in emacs will also work in shell - even macro definition and execution (ctrl-x(, ctrl-x), ctrl-xe). This is implemented through the ReadLine library, so most of those bindings will also be available in other programs using ReadLine.

Maybe you prefer vi bindings? Try set -o vi