Pimp your bash! (Part 2)

And now let’s move on to ~/.bashrc.

In part 1, we explored the almighty .inputrc file. Now it is time for our good ol’ neighbor, .bashrc.

There is plenty of information out there for the .bashrc file and what can be done with it. Much of it can now be considered common tips and tricks. I want to only focus on tips you may see less often or not at all:

1. Auto cd into a path

shopt -s autocd

This one is great. No more needless typing of the cd command. Just type the directory path and bash will cd into it.

2. Slim down your history file

export HISTIGNORE="cd*:ls*"

This is a colon-separated list of command patterns that you do not wish to go into the shell history file. There’s no need to save cd and ls commands for instance.

Here’s my own pattern string: "cd*:l[s|l]*:clear:exit:.*: *".
This will disable history for the cd, ls, ll, clear and exit commands. In addition it disables history for commands that start with a . (dot) (for a couple of alias I have). Finally it also disables history for commands that start with a space. If I don’t want a certain command to be sent to history, I can just start it with a space followed by the command (never really used it, but it’s there just in case).

3. Show your shell history date and time

export HISTTIMEFORMAT="%d/%m/%y %H:%M:%S > "

There’s no reason why you shouldn’t have this on your .bashrc file. See your shell history with the history command with each command timestamp (when it was issued). Neat!

4. Disable terminal suspend feature

stty -ixon

This is one of those well kept secrets of terminal emulation. You can issue this command on-demand, or just save it in your .bashrc file. I prefer the latter. What it does is disable terminal suspension with the ctrl-s key combination by enabling XON/XOFF slow control. This effectively allows you to use this key combination in your programs. Otherwise every time you hit ctrl-s, your terminal will lock and you need to hit ctrl-q to unlock it.

You know what programs like this setting? Well, Vim for one. In it, ctrl-w_ctrl-s is used to split windows. Another one is while doing reverse incremental search in your shell history file with ctrl+r. If you overshoot your target, you can reverse the search with ctrl-s and this will no longer lock your terminal. But you are now also free to map this keyboard combination to any other terminal program of your choosing.

5. Display your path as a vertical list

path() ( IFS=: ; printf '%s\n' $PATH ; )

We all know how the echo $PATH command displays our PATH environment variable. What an unreadable mess! This one is cool, it displays your path as a much more readable vertical list. From now on, just invoke path on your terminal to see your $PATH in full HD!

Upcoming in the series!

Colors in your terminal. How do they work, dammit? And explained as if you were 4 years old! Woohoo!
Why?.. Because we will then move on to setup that shell prompt of yours like a champ! So don’t die just yet.

10 Likes

Thank you @marfig. I’m really enjoying your “Pimp your bash!” series. Wonderful :smiley:

2 Likes

Those are great tips, thanks!