Or, How to Be a Command-Line Commando
Does it surprise you to learn that I’m a Linux guy? I’ve been using Linux, to the exclusion more-or-less of everything else, since about 1999. In the past, I’ve done a little programming and some junior system administration. I’m even LPI-certified.
With this background, I’m quite comfortable working in the shell (AKA the command-line), the natural habitat of the sysadmin[1]. I frequently open a shell to do some quick work, and when I do, I use GNU’s Bash, which is the default on most Linux distributions. (I believe it’s also the default shell in Mac OS X.)
One of Bash’s features is editable command-line history, which makes your current command-line and its entire history available to you as an editable buffer. That offers a great way to streamline your work in the shell.
I suspect, however, that many shell users don’t even know about this better way. And it baffles me that many SAs I have seen in action — including some of Pythian’s own — don’t use this. They almost seem to prefer unnecessary effort — smashing away at their keyboards, repeating themselves, deleting with the Backspace key, scrolling, forwarding their cursor one character at a time, copying and pasting with the mouse, and so on. That’s a lot of elbow grease.
With Bash, or any other shell that uses the GNU readline library, you can use the following Emacs-like key-chords to make your life better. The point of this (as with so many things sysadmins and programmers do) is to save you effort, viz. typing. These aren’t all of them; they’re the ones I use:
Keys | Effect |
---|---|
CTRL-P |
go to the Previous command in your history |
CTRL-N |
go to the Next command in your history |
CTRL-R |
Reverse-search through your history |
CTRL-S |
Search forward through your history |
CTRL-A |
Move the cursor to the beginning of the line |
CTRL-E |
Move the cursor to the end of the line |
CTRL-W |
delete a Word backwards |
ALT-D |
delete a word forwards |
CTRL-F |
move the cursor Forward 1 character |
CTRL-B |
move the cursor Backward 1 character |
ALT-F |
move the cursor Forward 1 word |
ALT-B |
move the cursor Backward 1 word |
ALT-_ |
undo |
It takes a little learning to get these under your fingers, but it’s worth it. Too often, easy-to-use GUIs get us started quickly, and then leave us handicapped in our work. Spending the time to learn how to use capable tools pays off.
If you don’t want the default Emacsisms, there’s a vi-mode too — try set -o vi
. I am a vi guy (in addition to a Linux guy), so I don’t mind modal editing, except that in the shell, there’s no way to tell which mode I’m in at any moment. Some commando out there might have put together a fancy custom PS1
prompt that shows vi-modes, but I really haven’t gone searching.
If you’d like a more thorough read about this, try the Bash Emacs Editing Mode (readline) Cheat Sheet or “man readline”.
1. As it happens, knowing how to use Unix text tools such as grep
, sed
, and wc
(to name just three) is very helpful to a writer, too.
7 Comments. Leave new
You have made an old man very happy. I have 789 customers scattered around Europe still scratching heads, desperately trying to fathom that I added ‘set -o emacs’ to ~/.kshrc.
Do add alt-.
It’s the history for the last argument. Example, tab-completing to this line:
ls /usr/local/bin/emacs-24.1
When you have 3 other emacs versions in there. Now it comes:
ln -sf m-. m-. BSBSBSBSBSRET
I bet most of you would either tab-complete or use the mouse.
Importantly, Ctrl-Y allows you to paste the text that you just deleted with any of the above key combination.
Ctrl-k: delete from cursor to end of line
Ctrl-u: delete from cursor to beginning of line
Ctrl-y: paste deleted part
Besides the simple yank with Ctrl-y, I really like that there is an actuall kill ring, so you can yank the previous kill with Alt-y…
Also, emacs-mode is most effective if you touch type, and press the Ctrl keys with your hands. I started using rather Ctrl-m to fire off my commands at the shell (instead of reaching all the way to RETURN).
I stumbled upon this looking for a way to reassign some of the keys, especially the delete-backward function. Found this article,
https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/
Turns out, its already assigned to Ctrl-H.
So no need for backspace either.
Oh, happy day…
Hi!
I’m a vim fan so I want to use vi key bindings everywhere :)
I use zsh with vi bindings.
In zsh I managed to set up a prompt that displays what vi-mode I’m in. (it chanegs colour of prompt)
You can check it on github:
https://github.com/szevzol/zsh/blob/master/rc/prompt
Limitation is that it doesn’t work in earlier versions of zsh. To be honest I only tried in 4.3.
If you use the latest zsh my approach should work :)
the cheat sheet of screen utility was very useful for me. Waiting eagerly for VI’s cheat sheet now.