May 26, 2018

Linux Shell Editing Shortcuts - page 2

Tips and Tricks of Power Shell Users

  • January 15, 2009
  • By Akkana Peck

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.

$ history
gives 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 15
shows 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:

$ !27
will 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

$ !gcon
and 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 :p after it.

$ !r:p
will 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, and see the command that way. I don't know of any way to do that in bash.)

Using pipelines with history

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.

$ history | grep man
will give you a list of all the times you've run the man command. Very useful!


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.

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

Most Popular LinuxPlanet Stories

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.