Far Beyond Programming — Braindumps by Eric Teubert

10 Terminal Tricks to Improve your Productivity

24 July 2010

I’ve been using RescueTime for a couple of months now. It’s a tool claiming to improve your productivity by tracking how much time you spend in which application. At the end of the day you get some fancy looking statistics. It doesn’t really track how much time you were productive. However, it’s interesting to see the time spent in certain applications. I, for my part, spend most of my time in TextMate. The terminal is ranking second. As I’m always looking for ways to improve, I’m primarily focusing on the tools I use most of the time. But it’s important to profile first, then look for improvements for the major modules. Otherwise it’s like premature optimization of code. Just don’t do it.

Enough analogies for now. Here are my top 10 terminal tricks:

  1. Create an alias to create aliases

    It makes sense if you read it twice, trust me :). For a long time I have known how useful aliases are. But it happened too often that I thought “That’d be a command for an alias! I’ll add it later …” and never did. This trick’s aim is to remove all barriers and make it as easy as possible to add new aliases. The alias looks like that:

    # in your ~/.profile or ~/.bashrc
    
alias newalias="$EDITOR ~/bin/dotfiles/bash/aliases &"

    Your path will be different but you get the point.

And while you’re at it, stop a second and think about your current major project paths. Add aliases for them and enjoy the saved time when you next type cdp (or whichever name you choose) instead of cd \~/projects/foo/bar

  2. Examine your history

    Type history, browse through the lines for commonly used commands and add some aliases. These might be source control commands or anything else you can think of. It first and foremost depends on what you’re doing in your terminal.

  3. Set your favorite editor as default

    There are a couple of situations where editors pop up while working in the terminal, e.g. when you have to write a commit message. To make this experience as convenient as possible, this should be your favorite editor. You can set it in your terminal config file. For me, this is TextMate: 


    # in your ~/.profile or ~/.bashrc

    export EDITOR="mate -w"
  4. Adjust the prompt to your needs

    Is there some information you have to look up frequently? You should think about putting it into your prompt. I like to have some basic version control system status there. You can set the prompt layout by changing the shell variable PS1:

    
# type in terminal or insert into your
    # config file for permanent change

    PS1=”\u: \w \$(vcprompt)> ”



    More information about vcprompt can be found here: http://vc.gerg.ca/hg/vcprompt/

  5. Learn keyboard shortcuts

    There are some gorgeous time savers waiting to be used. These are some of my favorites:


    Ctrl-a     move cursor to the beginning of the line
    
Ctrl-e     move cursor to the end of the line
    
Ctrl-u     kill everything before the cursor

    Ctrl-k     kill everything behind the cursor

    Ctrl-w     kill one word before the cursor
  6. Look for more shortcuts

    Type bind -p and browse through the output. Maybe you can find a hidden gem?

  7. Send long outputs to your favorite editor

    Some texts are just too long to read them in the terminal. Sometimes it’s helpful to look at it in an editor. My favorite example is the rails command to print all the routes. It looks awful in the terminal. However, pipe it to TextMate, set the language to Ruby and voilà, it looks terrific: rake routes | mate

  8. Rename a file

    It’s a rare case but it’s convenient to know how to do it when you need it. Say you need to rename a file that’s far away in terms of directories and you don’t want to cd there. The obvious solution would be

    
mv some/far/away/path/file.foo some/far/away/path/file.bar

    

But there’s a much shorter way to achieve the same: 


    mv some/far/away/path/file.{foo,bar}
  9. Ack

    Honestly, I can’t imagine living without ack any more. It’s just too useful. It’s like grep, only better. Much better. Please visit their site for more details.

  10. Mac OS X: Move cursor word by word

    To move the cursor one word backwards you have to press Esc-b and to move one word forwards Esc-f. That’s kind of insane as these are two great navigation tools. I prefer to have them attached to option-left and option-right. 

That’s how you do it: Open a terminal window and press Command-, to open the preferences. Go to Settings > Keyboard and activate the “Use option as meta key” checkbox. Now edit the “control cursor left” entry by changing the action to \033b and the “control cursor right” entry by changing the action to \033f. To get \033 you have to press Esc.

That’s it! These are some of my favorite terminal productivity tricks. I hope you found them useful. If you liked that post, please feel free to subscribe to my blog via RSS or Email. Do you know interesting terminal tricks? I’d be happy to read about them in the comments!

Fork me on GitHub