Last time, I talked about effective window management and my personal practices. In this post, I want to talk about what you can do to improve your experience in a terminal window.
Note: This post focuses on Unix shells (i.e. bash, mainly) and applies to Linux/OS X users. If you’re using a Windows command prompt, you’ll have to use Google to find alternatives. I’m afraid I haven’t used a Windows prompt enough to give advice. Also, I’m going to be a tad bit lax with terminology for the sake of readability.
Part 2: Terminal Tricks
Where to begin? There are a ton of things you can do to make your terminal more usable.
Learn your basic commands.
“Know your basics” sounds like obvious advice, but sometimes there’s a “basic” command that you just never heard of. It happens. Lifehacker has a great article covering some essentials.
Learn more about your shell, and what exactly is going on when you enter commands.
It’s not essential that you know exactly what you’re doing when you’re running terminal commands (yeah, I said it), but knowing some background on what’s going on when you type a command and press enter can be useful. Google is your friend, and the difference between a shell and a terminal/console is explained well in this StackExchange answer.
You should at least know what bash is, and learn about standard input/output.
Discover man pages.
If you’re ever clueless about how to use a command, or why it isn’t working as expected, take a look at its manual page by typing `man <command name>`. These pages provide detailed explanations of the program’s syntax and features.
Use tab completion!
When you’re typing a directory path or anything in your PATH variable (Google if you don’t know what that is), bash can autocomplete it for you if you press tab. This will save you loads of time – if you’ve ever seen someone typing at mach 4 on a terminal, chances are they’re just making good use of tab completion.
Discover the .bash_profile/.bashrc files.
These files are scripts that you can place in your home directory, and bash will source them when it starts. This means that you can add commands in this file, and they will be run in order whenever you open your terminal. This is great for aliases and such (see below).
Use aliases for common tasks.
If you find yourself typing a not-so-short command more often than you’d like, aliases are your friend. An alias lets you map a (short) command to another (longer) one. Here’s an example:
alias ..='cd ..'
Now, you’ll be able to just type “..” to go up one directory. Put this alias in your .bash_profile to have it set by default.
Remember to use single quotes here! Single and double quotes do different things –
when you use double quotes, the quoted command is run and its output is stored in the alias.
EDIT: I wrote this completely wrong (thanks clokep). What I was thinking of is that if your alias has a sub-expression and you use double quotes, the sub-expression is evaluated only once, when the alias is set (i.e. it isn’t escaped). If you use single quotes, the sub-expression is stored and evaluated every time you run the alias.
What’s a sub expression? You can include the output of any bash command in another using these:
There are plenty of uses for this. For example, many daemon programs (=programs that run in the background, like servers) create a “PID file” somewhere that contains the process id. You can then kill the daemon using something like this:
$kill $(cat /path/to/pid-file)
In many cases this can be substituted by piping output (I talk about this later in the post), but it’s a nice trick to know. Anyway, back to aliases…
There are a lot of great aliases out there, search around and set up your own!
You can have shortcuts for folders too, not just commands (though these aren’t called “aliases”). Just put something like this in your .bash_profile:
Now whenever you want to switch to your Dev directory, just type in `cd $DEVDIR` (maybe setup an alias for this?). Presto. This is obviously more useful for longer paths.
The reason you need a $ symbol is because you’re actually setting a bash variable, and this is how bash variables are referenced (by prefixing a $).
Colourize all the things.
Coloured output makes a huge difference when you’re trying to view directory contents or look at diffs! It’s easy to enable colours – just insert the following snippet in your .bash_profile:
# Pretty colors! export CLICOLOR=1 export LSCOLORS=GxFxCxDxBxegedabagaced # Pretty colors in less alias less='less -R'
This is for the colour scheme I use, you may want to customize it to your liking. Search around on how to do this, I don’t really remember the syntax for LSCOLORS myself. 😉
Note: Lines in bash scripts that start with a # symbol are comments. See below for more info about `less`.
Learn how to use output formatting/parsing programs.
The output of any command you run can be piped to another, meaning that it will be fed as input to the second program. The syntax for this is below:
$foo | bar
This will take the output of `foo` and give it to `bar`. This opens a realm of possibilities to format and parse output. Here are some useful programs you can use:
- grep – This tool lets you filter the output of a program and display only lines matching a pattern. For example:
$ls | grep foo
This will show you all files and folders in your working directory which contain “foo”. To make grep even more useful, you can use regular expressions (Google is your friend) for powerful pattern matching.
- less – This lets you scroll through long output rather than dumping it all at once.
- pbcopy – An incredibly useful tool on OS X that takes the output of a command and puts it in the clipboard for your pasting convenience. Linux users, see this.
- sed, cut, awk – Powerful parsing tools that are very useful for scripting. Refer to their man pages for more info.
You should also learn how to use > and >> to write output to files:
$ foo > bar.txt # Write the output of foo to bar.txt, overwriting any existing content. $ foo >> bar.txt # Append the output of foo to bar.txt.
To infinity and beyond!
The stuff above is nowhere near exhaustive. There’s always another neat trick you don’t know about, waiting to be discovered. Here are a few things you should check out:
- Keyboard shortcuts – bash has a load of nifty shortcuts waiting to be discovered. OS X users: see what happens when you Option+click. You’re welcome.
- Try a shell other than bash. I use zsh – it’s got superior tab completion and oh-my-zsh makes it easy to customize themes and put useful info in your prompt (I particularly like that I can see version control info, like the current branch, right in my prompt).
- Learn how to do basic on-the-fly file editing from a shell – `touch` and `nano` are tools I find useful.
- You can quickly do basic process management from a terminal: `ps aux` lists current processes, `kill` kills processes by id, and `killall` kills processes by name. Look at their man pages for more uses.
- Write scripts to automate compiling/running your project. There are plenty of resources on the internet to teach yourself how to write bash scripts.
That’s all for now! Stay tuned for more posts.