Shell Shortcuts

By | 2008/01/21

I was tearing away this morning configuring something in the shell. My wife peaks over my shoulder and says “How do you do that?!” I have a nice laugh and there was born an idea for my next post. This post outlines
some basic shell shortcuts. note: these use the default shell bindings, based on emacs.

Shell Shortcuts

First lets start with some simple stuff.

Clearing the screen. I still see a lot of people using the old ‘clear’ command to do that. A shorter method is:


Exiting the shell or logging out. I’m sure the most common way to exit your terminal session is using the ‘exit’ command. This can also be done with:


Searching the history. I’m guessing you’re familiar with using the up/down arrow keys for finding previous run commands. Did you know you can search through your history too, helping avoid hitting up a hundred times.

ctrl-r + command

The above will find the most recent match. If you’d like to go further back hit the key sequense again and it’ll toggle through matches of ‘command’ deeper into your history.

Using the previous argument. This is one of my favorites, and really can save some typing and monotony. You can drop in previously used arguments to your current commands with:

esc-. or alt-.

These are all that came to me off the top of my head. What other shortcuts do you use for the default shell? I’ll have a future post about set -o vi, so if you’ve got tips for that hold them for now.

You also might be interested in an earlier post on using copy and paste within the terminal.

9 thoughts on “Shell Shortcuts

  1. Robin

    Thanks, I didn’t know esc-. or alt-., I always used !$ for that!

    There’s of course ctrl-a, ctrl-e, ctrl-w and alt-f (which I have mapped to ctrl-f, for easier combination with ctrl-a/w).

  2. Gabriel Patiño

    Also usefull when using a slow connection, or reseting a password input:

    ctrl-u, clears to the begining of line

  3. Jeff Schroeder

    This is from a tomboy note I’ve got named:

    !! – Repeats and runs previous command
    echo !$ – will echo the last argument of the previous command
    !ssh – Runs the most recent command that begins with ssh
    !vi:p – Run the most recent vi command and only print it. This does not run the command.
    !vi:s/passwd/shadow/ – Run the most recent vi command replacing passwd with shadow
    !?passwd – Runs the most recent command that contains passwd anywhere

    ^file1^file2 – Repeat the previous command and replace the first instance of file1 with file2

    CTRL E – end of line
    CTRL A – beginning of line
    CTRL Arrow (left or right) – left or right by 1 word
    CTRL R – reverse find as you type command history search
    CTRL W – delete the word to the left of the cursor (space separated)

    ALT Backspace – remove 1 word in a string. ie:
    cd /etc/ssh/authorized_keys ALT Backspace would remove authorized_keys

    Those are the main ones I use on a daily basis

  4. guignome

    I often use echo $? to get the return value of the last command. If it’s different from 0, it means that something went wrong.

  5. tomchuk

    Add to your ~/.inputrc :

    “\e[5~”: history-search-backward
    “\e[6~”: history-search-forward

    Type the first few chars of a command and press page-up/page-dn

  6. Kai Grossjohann

    I think it is worth saying that the argument of Ctrl-R is a substring to search for. That is, Ctrl-R x searches backwards for shell commands that have x anywhere in the line.

    It is also worth noting that M-. (ESC .) can be repeated: The first time, the last word of the previous command is inserted, hitting it again replaces it with the last word of the command before that, and so on.

    I like to use “!!” a lot, and I combine it with strings. For example, I might type “ifconfig eth0” to find the current IP address, only to discover that on this box, /sbin is not in $PATH. So I type “/sbin/!!” as a shortcut for “/sbin/ifconfig eth0”.

    Another shortcut I would like to use is Ctrl-O, but I’ve not yet been able to train myself. Say you type five commands, then you want to repeat the same five commands in order. Then you could type to find the next command to repeat. Or you replace with Ctrl-O, and then you will see the correct “next” command in the command line.

    Here’s something that I use: “!ma” to repeat the last command that begins with “ma”, which most of the time will be some invocation of “make”.

  7. philipp

    I do:
    set -o vi
    and use all the wonderfull vi-commands.

  8. Hans

    ^L is not a shell trick. If it works, it works because your terminal emulator wants it to. It doesn’t in mine. It does however initiate a redraw in most curses-based programs (mutt, aptitude, etc.).

  9. Erik

    Closely allied to !$ is !*, which bash expands into all of the previous command’s arguments.

    Suppose you use the command `ls a b c d e`, and following that, you wanted to do a `rm b`.

    That’s accomplished with `rm !:2`. The !: construct lets you sniper out specific arguments from a previous command, numbered like argv[] items.

    Going further with the example, can even say `rm !:2-4`, which expands to `rm b c d`.

    Check out section 9.3.2 in the bash manual for more on “Word Designators”.


Leave a Reply