1. Home
  2. Linux

How To Edit The BashRC File To Make Bash More Useful On Linux

The Bash Shell is impressive, but it could use some work. In this guide, we’ll go over ways you can improve your terminal experience by modifying and improving the BashRC file. Before you edit the BashRC file, please run this command in terminal to safely back it up:

cp .bashrc bashrc-bak

History Completion

One of the killer features of Bash alternative “Fish”, is it’s ability to quickly figure out what you’re typing while typing it. This feature makes it super easy to remember past commands and operations with ease. Unfortunately, Bash can’t do this out of the box, and if you want to remember a command, you’re stuck viewing the history file — until now.

As it turns out, there’s a way to mimic this awesome Fish feature in Bash with a simple edit to BashRC. Open the file with Nano and add the following to enable quick history searching;

nano ~/.bashrc

Inside of your BashRC file, look for “#User specific aliases and functions” and paste the following code underneath it. Keep in mind that many Linux distributions customize the BashRC file, so you may not find this line. Generally speaking, the code should go to the very bottom of the file.

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

The code above will bind an ability to quickly go through Bash history by clicking left or right arrow keys on the keyboard.  Press Ctrl + O to save, and Ctrl + X to exit. Close the terminal and re-open it to see the changes you’ve made.

Better History Logging

Like most terminal Shells, Bash saves a file with everything you type into it for convenience. This is certainly a useful feature, but it gets annoying sorting through the file because of duplicates. Having the same command show up over and over can make finding the things in the Bash history you do want to see more difficult. To solve this problem, consider adding this edit to ~/.bashrc that actively removes duplicate commands, improving how Bash handles command history.

nano ~/.bashrc

Inside of Nano, paste this code:

export HISTCONTROL=ignoredups

Zsh-like Command Help

In the Zsh shell, it’s easy to open up any terminal command’s manual by quickly pressing Alt + H on the keyboard. With Bash, accessing a manual is man command. Suffice it to say, the Zsh way of doing things is much, much nicer. To add this functionality to the Bash shell, add this to the bottom of ~/.bashrc.

bind '"\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"'

Auto CD

An excellent feature Zsh has is the ability to automatically CD into a  directory. Instead of invoking cd somedirectory all the time. Though using the CD command inside of Bash certainly isn’t hard, it can be tedious typing it over and over, to navigate everywhere.

Open up your ~/.bashrc file with Nano and paste this code inside of it.

...
shopt -s autocd
...

Save the edit with Nano by pressing Ctrl + O, and exit the editor using Ctrl + X. Close the terminal, and reopen it. From now on, to move to a new directory, type the folder path without the cd in front of it. For example:

/usr/bin/

Improve Tab Completion

One of the best features of Bash that a lot of users don’t know about is Tab completion. By default, Bash can automatically complete a command. To use it, start typing out the first few letters of a command, directory, etc, and Bash will fill out the rest. The problem is that this version of tab completion needs work. It isn’t perfect, misses things, and flat out refuses to work sometimes.

Luckily, there’s a quick way to improve tab completion for Bash. Unlike other edits in the guide, this section is quite easy and only requires the installation of a package. Open up a terminal and install the bash-completion package.

Ubuntu

sudo apt install bash-completion

Debian

sudo apt install bash-completion

Arch Linux

sudo pacman -S bash-completion

Fedora

sudo dnf install bash-completion

OpenSUSE

sudo zypper install bash-completion

Resetting BashRC

In this guide, we’ve made a lot of changes to the ~/.bashrc file. As a result, your Bash terminal operates differently. These modifications no-doubt make Bash more modern and useful, but not to everyone. Some may prefer the way Bash works without the modifications.

Luckily, at the start of this guide, we created a backup of the original ~/.bashrc file. Creating a backup makes undoing the edits made in this tutorial very easy. To restore the backup, open up a terminal and start by deleting the new ~/.bashrc file.

rm ~/.bashrc

After getting rid of the modified file, it’s safe to restore the old backup.

cp bashrc-bak .bashrc

Running this CP command will restore the file to its original state. Restart your PC to finalize the changes.

Leave a comment