October 9th 2007

Top 10 interactive shell anti-patterns

In general, if you're doing a repetitive task by hitting lots of keys, you're doing it wrong. Not only are you wasting time, you're wearing out your keyboard and damaging your wrists and fingers.

Here are the ten most common anti-patterns (actually amelioration patterns) that I commonly see in other people's line editing:

  1. Repeating a command by retyping it

    Press Ctrl-p and Enter, or "!!". Zsh users get "r" as well. You can even get the command n times before, use "!-1". Using the arrow keys is obviously flawed - find out how to unmap them in your shell.

  2. Manually changing back to your previous directory - Give "cd -" a whirl.

  3. Typing a long command (or sequence of commands) excessively often - Obviously wrong, but I see it so often it's worth mentioning. Try using an alias and put it in your shell's startup file. If the command is parameterised, you could construct a simple function. If you are writing software, look into the build systems available for your language - the basics of GNU Make can be mastered within 20 minutes or so, and have many other benefits too.

  4. Opening another shell when the current process blocks - (e.g. You realise the command you started is going to take a lot longer than you thought) Pause the current process with Ctrl+z and then put the job in the background with bg. fg will bring it to the foreground again. Fewer shells mean you stay focused on what you were trying to achieve.

  5. Searching history by grep'ing ~/.bash_history, or Up Up Up ... etc. - Use CTRL+r (or "/" in vi-mode) for search-as-you type. You can immediately run the command by pressing Enter.

  6. Adding a prefix to a command with Up Home $command Space - (e.g. You ran a command that really needed sudo). Try: $prefix !!

  7. Changing a command with Home Right Right Right Right ... BkSpace BkSpace ... ``etc. - (e.g you accidentally ran ``chown instead of chmod) Try: $new_command !*

  8. Retyping significant parts of the previous line

    $ command arg1 /long/path/to/file
    $ command2 arg2 /long/path/to/file
    $ command3 arg1 arg3 /long/path/to/file
    

    Try using !$ to get the right-most argument on the previous line (you can also use !:2 to get the second argument, etc.):

    $ command arg1 /long/path/to/file
    $ command2 arg2 !$
    $ command3 arg1 arg3 !$
    
  9. Using Home/End to move to the beginning/end of the line - This is how you completely ruin your wrists. Use Ctrl+a and Ctrl+e.

  10. Hitting tens of keys to make a substitition* - (e.g. you made a trivial mistake in the middle of your previous command). Use ^foo^bar. If you forget the other nine, try this one - stop punishing yourself for making mistakes.




You can subscribe to new posts via email or RSS.