Top 3 Shortcuts for the Terminal

| Comments

As my coworkers know, I’m a keyboard shortcut junkie. It’s a problem (and I need help), but I’d rather give you a taste of what I’ve been smoking and drag you down with me :).

Why only 3? Because people don’t learn a couple of pages of keystroke combinations at a time. But with only the 3 of the best keystrokes, there’s a good chance I’ll get you hooked and you’ll seek out some more.

  1. Incremental Search Through Command History - ctrl+R
  2. Most people know that pressing the up arrow at a prompt will go to the previous command, and subsequent up arrows will move backwards in history. That works fine for commands that you just typed. What if you had checked out the grails trunk a week ago and wanted to do it again, but couldn’t remember the exact repository name. Just type ctrl-R and then a part of the command that you can remember, such as svn co. As you type it will search backwards through your command history for a command that matches. When your comamnd appears, just hit enter to execute.
    svn co grails
    bck-i-search: svn co_
    If there were multiple commands that match the pattern that you typed, either type more of the command to get more specific, or hit ctrl+R again to find the next most recent command that matches the pattern.
  3. Delete Last Word - ctrl+W
  4. There are a number of great movement and deletion commands (almost all of them originating from emacs keybindings). But ctrl+W is the one I use more often than any other. It deletes the previous word On the mac, one other benefit of knowing these keybindings is that they work in just about every cocoa based application as well as a few other apps. For example, in Firefox, ctrl-W will work on the location bar to make it easy to delete parts of an URL. Once these commands become part of your muscle memory, you’ll find yourself using them all over the place.
  5. Insert Last Argument of Previous Command - option+. (period) or esc+.
  6. How many times have you wanted to do 2 things with a file or directory in succession? It happens all the time to me and this is my favorite command that I’ve never seen anyone else use. For example, if I want to do a quick update of one of my domain classes from subversion:
    svn up grails-app/domain/Author.groovy
    and then edit it in TextMate, all I have to do is type
    then press option+. and it autocompletes to:
    mate grails-app/domain/Author.groovy
    There is a word designator shortcut, ”!$”, that also means “last parameter of previous command”, but with ctrl-. you actually get to inspect and edit the parameter easily. Also, on OSX, esc is the default “meta” key, but you can (and should) go into the terminal preferences and check the “Use option as meta key” checkbox on the Keyboard tab.

If you’re interested in learning more about terminal history commands, I highly recommend The Definitive Guide to Bash Command Line History.