Using the Terminal – Part 3: the history and aliases

Using the Terminal – Part 3: the history and aliases

In the third part I will continue with some more basics. Now that you know a few commands you might want to know how to use them easier.

I promised to tell you a bit more about the history and how to use it more efficiently. The history is created by the shell, so it does not depend on the terminal application you are using. Next I will tell you a bit about aliases and how you can use them to make the terminal a little more your home. The aliases are also a function of the shell.

What is the history?

In fact, the history is just a text file stored in your home directory called .bash_history. Yes, it is that easy. A simple plain text file that you can open with your favorite text editor, you can even delete or change its entries. Each line represents a history entry.

How to control the history?

The history is controlled via the file .bashrc (it controls a lot of different shell features) and you can set various options to make the history more effective. It’s a simple text file so you can edit it with your favorite text editor, anyway, you should make a copy of the original file before starting to change it. Many options are already available, sometimes you just have to remove the hash (#) in front of the respective setting.

Changing HISTSIZE isn’t really necessary, for normal use the history is large enough.

The value HISTCONTROL is already set to ignoreboth, that is a combination of the values ignorespace and ignoredupes. ignorespace means that any command you type that begins with a space will be ignored. ignoredupes will stop the history from adding duplicate entries when when you enter the same command more than once in a row.

One thing you could do is to exclude certain commands, that is done by adding a line like this
HISTIGNORE="cd*:ls*:*--help:man*:clear"
to .bashrc. This would exclude all commands that start with cd, ls and man and all commands that end with --help as well as the command clear. Why? Cause they change nothing, so it’s not worth to remember them. You can also add you own aliases to the ignore list.

You can also add timestamps to your history entries by adding
HISTTIMEFORMAT=”%FT%T ”
to .bashrc, this would create an ISO8601-like timestamp (YYYY-MM-DDThh:mm:ss).
Screenshot from 2015-04-12 19:20:42

In fact you can do way more with your history than this but that is the part where it really starts to get complicated. Getting the behavior of the history to be exactly as you want it to be can be a bit of a pain in particular when you run multiple terminals.

Using the history

This is the by far most important part. There are multiple ways to use the history. One is the history command that just lists all the history entries, with a thousand entries that is a lot to scroll. You can also use the arrow keys (up and down) to browse the history, that is useful for recent commands but not for commands you used a few days ago.

One simple solution is to use history in combination with the grep or the egrep command. grep is a search command and you can filter the output of history with grep using a so called pipe with the the output of the former being used as the input of the latter. The egrep command is a bit more powerful. That sounds more complicated than it is:
history | egrep [searchterm]
So searching for all commands that contain zip use
history | egrep zip
This is particularly helpful in the beginning cause often remember a command you used vaguely.

Each entry has a number (n), you can use that for example to perform the exact same command again typing !n, you can even expand that line. That is really useful when you forgot to sudo a command, for the last history entry this is even easier, cause you can use !! to execute it again, so you just have to type sudo !!.
Screenshot from 2015-04-08 13:43:28
If you do not feel comfortable with the command being executed instantly you can add
shopt -s histverify
to .bashrc so the command will expend so you can check it before you execute it.

Another great method to search the history is the built-in search function (reverse-i-search) that you can open by pressing CTRL+R. It’s a typeahead search, so you just have to start entering the term you remember. To jump to earlier commands you have to press CTRL+R again. It is also possible to jump back to later commands using CTRL+S (called i-search) but that is by default not working, you can enable it by adding stty -ixon to .bashrc and switch off another feature or bind it to another key using bind '"\C-f": forward-search-history' for bind it to CTRL+F (it is usually used to do the same as the arrow-left key and therefor redundant).
Screenshot from 2015-04-12 18:47:44

What is an alias?

An alias can do lot’s of different things but but they all have in common they are meant to make things easier and adjust to your liking. You can for example use an alias for your favorite way to call a command. But you can also hide larger code snippets behind an alias like the alert command mentioned in an earlier post.

Per default the *buntus already have some aliases pre-configured, you can list them by typing alias. You will see that ls as used in the *buntus is already an alias for ‘ls –color=auto’. The syntax to define an alias is pretty simple to make .. an alias for cd .. you can use the command alias ..='cd ..' directly in your terminal (try it) but this will only last until you close the terminal. To make it permanent you can add the alias to .bashrc or (better) to a new file called .bash_aliases.

What can I use an alias for?

Virtually everything. You already saw a few example above that could be useful. I like to have two versions of ls as an alias
alias l='ls -lhp '
alias la='l -A '
I also have an alias for gvfs-trash
alias del='gvfs-trash'
Just make sure that you have an alias that is short and easy to type and to remember, del (for delete) is much easier to remember than gvfs-trash. You can also make an alias like this
alias hist='history | egrep '
to make searching the history easier, you just have to type hist and the term you want to search like hist zip to search all zip commands.

You can also use an alias to make commands that are potentially dangerous a little less dangerous by making the interactive, e.g. the rm command
alias rm=’rm -i’

Tl;dr

Two things that make the terminal much more effective also – but not only – for beginners are the history and the aliases. The history let’s you lookup commands you already used and let’s you even reuse and modify them. With aliases you can create custom commands for actions that you often use or that are hard to remember. Both are features of the shell, so you only have to do them once and they are available in every terminal application you use.

Do you have a favorite alias or a simple little history trick you like to share?

Leave a comment