A few tips and tricks on the terminal to make you more efficient when using it. If you know of any other tips, add it in the comments section.
1. ls Without ls
When you are trying to cd into a deep folder, you might not know the correct folder name some levels deep. You might have to do something like…
$ cd ~/Scripts/Perl
$ ls
bin SedGUI ToSee Cronjobs Maintenance
$ cd Maintenance
There is an easier way – go to the wanted folder
$ cd ~/Scripts/Perl
Now, without pressing enter, double tap the TAB key. You will get a list of files. And the command prompt waiting to be filled…
$ cd ~/Scripts/Perl/[TAB TAB]
bin SedGUI ToSee Cronjobs Maintenance
$ cd ~/Scripts/Perl/_
You can also use double-TAB to auto-complete commands.
2. Searching the history with Ctrl+R
If you have to use a command you have already used before, press CTRL+R and then type a few characters of the command. The latest command with those characters will be shown – if that is the command you want to execute, press enter and it will be executed. If not, just press CTRL+R again and it will show the next command.
You have no idea how useful this tip is if you haven’t been using it. I use this all the time.
For more details, read this article.
3. Open Terminal using a Shortcut
If you are a GUI user, chances are you prefer using a Terminal emulator(like gnome-terminal
or konsole
) instead of going into the Terminal mode by pressing CTRL+ALT+F1. If so, assign a shortcut to those emulator apps. I prefer using the shortcut ‘Ctrl+Alt+A’ to do this.
Gnome
If you are in gnome, there is a very easy way to do this…
- Go to System > Preferences > Keyboard Shortcuts
- Find ‘Run a Terminal’ – assign the shortcut ‘Ctrl+Alt+A’
KDE
- Right Click on the K-Menu > Menu Editor
- Find your terminal application in the list(usually System > Terminal Applications > Terminal)
- Select the ‘Current Shortcut Key’ option and set it to ‘Ctrl+Alt+A’
You can also do this by opening the KHotKeys application.
4. Bash Keyboard Shortcuts
Learn the bash keyboard shortcuts – these are the ones I use the most…
- CTRL+R
- Search the history. We already talked about this.
- CTRL+L
- Clears the screen. Use this instead of the
clear
command. - CTRL+D
- Use this instead of the
exit
command. - CTRL+C
- Kill whatever is running
- CTRL+Z
- Puts whatever is running into a suspended background process. Use
fg
to restore it.
5. Find Command using apropos
Find the command you want using the apropos
command. Just type in a description of the command as the first argument. For example, lets say you want to find the command to list the directory contents. Use the command…
$ apropos "directory contents"
dir (1) - list directory contents
ls (1) - list directory contents
ls (1p) - list directory contents
ntfsls (8) - list directory contents on an NTFS filesystem
vdir (1) - list directory contents
The only problem is that I can never spell ‘apropos’ – so I keep this in my .bashrc
file…
alias apox='apropos'
6. Learn New Commands
There are a few sites that publish cool commands on a daily/semi-daily basics – subscribe to those and learn new commands…
A few more for the bash keyboard shortcuts (and a hint or two to go with ctrl-z/fg):
ctrl-a: Move the cursor to the start of the line. Much better than holding the left arrow for 30 seconds, especially over a serial connection. If you use screen, you can type “ctrl-a a” to achieve the same effect or change the screen command key combination to something else. (I use ctrl-s for screen rather than ctrl-a)
ctrl-e: Same as ctrl-a but it moves the cursor to the end of the line.
ctrl-w: Delete the whole word to the left of the cursor on the command line.
ctrl-s: Pause terminal output. The program keeps running in the background but the terminal just displays wherever it was up to. Handy for the middle of large “find” or “grep” operations.
ctrl-q: Releases the terminal again after using ctrl-s. This one is really handy to know if you accidentally hit ctrl-s and can’t figure out how to get your terminal working again.
To go with ctrl-z and fg are a couple of other commands: “bg” is the same as “fg” except that the program runs in the background instead of the foreground. The output still appears on the terminal and the program is still tied to the bash process (i.e it’s still a child of the bash process) which means it will be killed if you log out but you can type in the terminal and start new programs.
The “jobs” command will list all of the process you have that are suspended or running in the background. This list will have a number next to each process. You can fg or bg any process from a list by using the number after the fg or bg command. e.g. “fg 3” will bring the process numbered 3 to the foreground.
The “disown” command will disconnect a process from the bash session it was started in. This means the process will keep running even if you log out. Disown accepts the same numbers that fg and bg do from the jobs command.
Lastly, if you add an ampersand ( & ) after a command it will automatically start in the background.
Thanks – I did not know about Ctrl-S/Q – I think those two are very useful.
^k – erase to end-of-line
^u – erase to beginning-of-line
Shift+PgUp/PgDn – scroll through current buffer
In many cases, certain combinations like CTRL+Left/Right permit you to navigate on a word-by-word basis. This works often in editors as well as many terminals(though not all).
Very nice.
The Ctrl+R and Ctrl+Z feature are great.
I was looking for this kind of things…
Antoine