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
ctrl-e will move the cursor to the end of the command line.
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
will the remove the entire line of text, storing that in the cut
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
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
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
/var/tmp. If you as the next command type
cd Alt-., the shell will
/var/tmp into your commandline, resulting in
Alt-.’s will cycle backward in history.
!$ 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 larso@goethe:/var/tmp/foo$
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-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 …
Subscribe via RSS