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
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;
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
‘); if (navigator.appVersion.indexOf(“Mac”)!=-1) document.write(”);
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.
Inside of Nano, paste this code:
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-aebedC-ye#man C-yC-mC-pC-pC-aC-dC-e"'
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:
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.
sudo apt install bash-completion
sudo apt install bash-completion
sudo pacman -S bash-completion
sudo dnf install bash-completion
sudo zypper install bash-completion
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.
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.