Sunday, 28 June 2009

6 Bash Productivity Tips

1. Use !!
Typing !! followed by Enter will bring back and execute the last command (same as Ctrl+P followed by Enter).

2. Use Emacs-like shortcuts
Emacs-like shortcuts make use of the Ctrl and Alt keys, and the big advantage of navigating and manipulating text in a Bash prompt is that you won't have to move your fingers away from the typing position (to access the arrow keys, or the Home, Page Up and Page Down keys). Notice that most of those work well inside manual pages too, or when using the Nano editor, or the less and more pagers.

Ctrl+D - or EOF (end-of-file) will quit the current Bash session
Ctrl+P - bring back the last command executed
Ctrl+N - bring the next command executed
Ctrl+U - delete all from the cursor to the left
Ctrl+K - delete all from the cursor to the right
Ctrl+B - go back one character
Ctrl+F - go forward one character
Alt+B - go back one word
Alt+F - go forward one word

3. Make manual pages coloured
I think this tip is a productivity tip because once a manual page is shown coloured, it is easier, clearer to read. Just add the following inside the ~/.bashrc file, where ~ is your home directory (notice that each line starts with export):

export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline

Then, type:

source ~/.bashrc

And try any manual page (e.g. man bash). Here's how a coloured man page looks like using the presets above:

Coloured man page

4. Use your own scripts
If you have basic knowledge of Bash scripting, you can create scripts and put them in a directory included in your $PATH. You can use something like ~/bin or ~/usr/bin, where ~ is your home directory.

5. Copy/paste using Shift+Insert
Although this one is not Bash-specific, it's very useful when you need to quickly copy/paste some text in your terminal. The same can be accomplished using the middle-click mouse button.

6. Use aliases
One of the powerful features of Bash is that it makes use of aliases. Those are usually short commands which perform some associated, longer command or commands. For example, to update and upgrade your Ubuntu installation you would type sudo apt-get update && sudo apt-get upgrade, and to create an alias for it you would only have to add something like the following inside the ~/.bashrc file:

alias update='sudo apt-get update && sudo apt-get upgrade'

Now, each time you type update, the command inside the single quotes will be executed.

Have some other tips that others may find useful? Please share them in the comments below.


Anonymous said...

How colours help to understand better
man pages as black and white?

Anonymous said...

Colors don't help you understand man pages better (that depends on your IQ which I can already tell isn't very high). They help you READ man pages better.
All productivity tips here are for novices anyway.

Anonymous said...

Far more useful than #1 is the ability to rerun long commands that your forgot needed sudo.

Just try it:

$ whoami
$ sudo !!

Noah said...

Don't be so hard on the poster. Although a lot of this stuff is for novices, it might help someone. And by "understand," he probably means organize and process -- if you're just starting out reading man pages, having some colored guidance with respect to where to look might be pretty helpful.

Anonymous said...

Just a quick thanks to the author for this post. I've been using Linux for over eight years, and some of them I'd never come across.

The dude/dudette who differentiated between READ, and UNDERSTAND - don't mind him - he's an idiot, and he's wrong. I found some of these tips helpful, and I'm not a novice by any strech of the imagination.

Anonymous said...

Just to expand point 6 with an example of bash functions

function update {
sudo apt-get update && sudo apt-get upgrade

functions can also take arguments. Here's a bit of a contrived example, that simulates yum behavour.

function install {
sudo apt-get update && sudo apt-get install "$1"

Also bash variable syntax, is something I use quite a bit - try the following code:

MSG="Feed the cat."
echo $MSG
echo ${MSG/cat/hacker}

Anonymous said...

Please note that Ctrl-K and Ctrl-U does not "delete" text but "cuts" it (delete and copy). Once copied you can paste it with Ctrl-Y.


Christopher said...

What about CTRL-R, reverse search? It looks for the most recent command containing the text that you type in. Couldn't live without it.

alvare-ClrnD said...

ctrl+left and ctrl+right also to move by words.

Uncommenting/adding this to /etc/inputrc

"\e[5~": history-search-backward
"\e[6~": history-search-forward

will use page up/down to search trough history with the current text written.

Zona de Slumbergod said...

I hate it when linux geeks pollute blog postings with arrogant, stupid comments.

Why do you even bother reading blogs if you know it all already?

Thanks to the poster of the article. It's people like you that help create a friendly, supportive environment for new linux users.

Anonymous said...

I use linux now for some 5 years or so, but often learn something new from posts like this, even though there are always things one already knows, but that doesn't matter, how else can others learn the same tricks!. And yes, I do love bash, so the more handy tips the better! Thanks for this post

Sascha said...

I am using Linux for quite a while now and i am still surprised nearly every day what one can do by just some simple commands. To my mind coloring the man pages is a great thing. It just helps you scanning the text quickly and finding the things you are looking for. I always hated to read man-pages beacause of their looks.

There is always more to learn!

Thanks for the post!

Anonymous said...

Crtl-a and Ctrl-e take you to start or end of line