This post appeared originally in our sysadvent series and has been moved here following the discontinuation of the sysadvent microsite
![](/techblog/images/features/sysadvent/RPLP_Logo.png)
This post appeared originally in our sysadvent series and has been moved here following the discontinuation of the sysadvent microsite
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
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 prompt.
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 prompt. 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 sub-string match in reverse
chronological 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.
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 command-line, 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:
mkdir /var/tmp/foo
cd !$
cd /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-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
…
Unless you’ve been living under a rock, you probably know that last Friday a global crash of computer systems caused by ‘CrowdStrike’ led to widespread chaos and mayhem: flights were cancelled, shops closed their doors, even some hospitals and pharmacies were affected. When things like this happen, I first have a smug feeling “this would never happen at our place”, then I start thinking. Could it?
Our department do take responsibility for keeping quite a lot ... [continue reading]