When using the terminal, there are a lot of tricks and shortcuts that can make using the terminal much more efficient and pleasurable. I’ll list here some of the key ones that I use.
history
Often in a terminal, you end up repeating the same commands from time to time. There is a program that records each command you type into a terminal called history. However, typing history and then searching for a command is slow and tedious, and the antithesis of what this howto is about. For searching through recent commands, you can use the up arrow. This will cycle through previous commands in order, one at a time. However, this can get tedious if you’re looking for something you typed a while ago. There are better ways of doing this. ctrl+r will allow you to interactively search through history as you type in commands. Pressing ctrl+r at an empty command prompt will give you a prompt that looks like this:
(reverse-i-search)`': |
You can then type in part of a previous command and commands from history that match what you’ve typed will start to appear on the command line. So if I’m at the ctrl+r prompt and type up I get:
(reverse-i-search)`up': cd .superkaramba/lwp/ |
If I keep typing and add d, then the recalled command changes to:
(reverse-i-search)`upd': sudo aptitude update |
This will find the most recent command that matches the characters as you type them.
The other way to search history for a command is with the grep command. So if you want to find all commands where you’ve used install, then you would type:
matt@laptop:~$ history | grep install 20 sudo aptitude install slmodem 69 configure --prefix=/usr && make && sudo make install 70 ./configure --prefix=/usr && make && sudo make install 74 sudo aptitude install openoffice.org-common 107 sudo aptitude install basket 112 ./configure --prefix=/usr && make && sudo make install 342 sudo aptitude install caca 412 sudo aptitude install spamassassin 428 sudo aptitude install eric 488 sudo aptitude install avidemux 498 sudo aptitude install libmozjs-dev 505 history | grep install |
This can be useful where ctrl+r is not calling up the command line you want because a more recent command matches what you are typing into ctrl+r, and you can’t get to the previous command you want.
tab completion
Tab completion allows you to quickly type in filenames and directory paths on the commandline. When on the command line, you can type the first part of a filename or another directory in the current directory, and then press the tab key, and the rest of the filename will be completed for you. There are a few caveats:
- If there is more than one file or directory that matches what you’ve typed, tab completion will display all of the files that match what you’ve typed.
- If you use kubuntu, tab completion is context sensitive. So if you start typing tar -zxpf fil and press tab expecting it to complete filename.tar.bz2, tab completion will fail, as a tar.bz2 file is not compatible with the -z argument, which expects a .tar.gz file. This can be annoying for files that do not have an extension, or the correct extension.
cd
There are a couple of tricks with the cd command that are useful:
matt@laptop:~/tmp/Babasse$ cd matt@laptop:~$ |
cd on its own will put you back in your home directory.
matt@laptop:~$ cd - /home/matt/tmp/Babasse matt@laptop:~/tmp/Babasse |
cd – will change to the previous directory.