Linux Shell Editing Shortcuts - page 2
Tips and Tricks of Power Shell Users
Shells make it easy to repeat commands you've already typed, or to change a command a little then try it again. That's because shells maintain a list of the commands you've typed recently -- your "shell history" -- and give you a variety of ways to access it.
The first, and easiest: to see the last command, hit the up arrow key. You'll see the last command you typed, which you can edit or change in any way you like. It won't be executed until you hit Enter.
You're not limited to just the last command, though. Keep hitting the up arrow and you'll be taken to commands you typed previously. If you go back too far, down arrow takes you forward again, or just clear the line (with Ctrl-U) and hit Enter to start over.
The shell also offers a command to view your history in larger chunks.
$ historygives you a list of all the commands you've typed in a while. (Remember, the $ represents your shell prompt; don't type that part.)
If the list is too long, you can limit it:
$ history 15shows you only the last 15 commands you typed, as in Figure 1.
Each of these commands has a number next to it. If you see the command you're after, you can run it again just by typing an exclamation mark (called "bang" for short) and the number:
$ !27will run the 27th command, "gimp &", again, without needing to type lots of up arrows to get there.
You can also use a bang to find commands beginning with particular letters. In Figure 1, if I want to run that long gconftool command again, I need only type
$ !gconand the shell will run the last command it finds that started with those letters.
Notice that if I simply typed
!g, it wouldn't run
gconftool-2; it would run gimp, because I ran gimp more recently.
That makes these "bang commands" slightly dangerous: you don't
get to see the command and confirm it before running it.
Imagine you typed
!r intending to repeat a
rename command you remember running; except you'd forgotten
that since then you ran something dangerous like
rm -rf .
If you're ever in doubt what a bang history command might give you,
you can ask to have it printed first, by adding a
$ !r:pwill print the last command starting with r. Once you've seen it, you can run it by using the up arrow key, or by typing
!!, which runs the last command.
(Users of the tcsh shell get a nice bonus here: you can use the tab key
with history, like !r
If you've been experimenting with shell pipelines at all, you may
already see how you can combine history with grep to search
for a specific command you ran a while ago and don't want to type again.
Keybindings (Firefox) explains all the editing bindings (which are
basically the same as the ones the shell uses) and how to enable them
for GTK apps.
Reference Manual: Using History Interactively discusses the
details of Bash's history mechanism and how to configure it.
Sponsored by BlackBerry
Using pipelines with history
$ history | grep man
will give you a list of all the times you've run the
command. Very useful!
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!
If you've been experimenting with shell pipelines at all, you may already see how you can combine history with grep to search for a specific command you ran a while ago and don't want to type again.
Emacs Keybindings (Firefox) explains all the editing bindings (which are basically the same as the ones the shell uses) and how to enable them for GTK apps.
Bash Reference Manual: Using History Interactively discusses the details of Bash's history mechanism and how to configure it.
Sponsored by BlackBerry