Note: This tutorial is designed for the linux/mac terminal. If you're on windows using WLS/Cygwin, some commands may not work or maybe slightly different. If you use windows, you may want to read this for your .bashrc file.

Things about the terminal

When you type a command in the terminal, the command needs to be interpreted by the language, to know what to execute. This is known as the shell-scripting language. Some examples are, power-shell, bash, zsh and fish. We'll be using bash, since it is installed by default.

Most 'rc' files are used for configuration.

Based on the shell you use there are configuration files. Since these files start with a '.', they are also known as dotfiles.

The main dotfiles for the bash terminal are .bashrc and .bash_profile. The .bash_profile file is run every time you log in. In this tutorial however, we will mostly be tinkering with the .bashrc file which is run every time you open a terminal. Since dotfiles are hidden by default, you may want to enable viewing hidden files in your file manager, or use ls -A to list the files.

Getting the ugly things out!

The first thing to get a fancy and efficient terminal, is to configure a good default setup. This is done through the .bashrc file in the home directory. Since this file is run every time you open a terminal, you could put any commands you want to run before starting up the terminal here.
For example, adding the line firefox blog.bitsacm.in ( or google-chrome for chrome) to the end of your .bashrc would open firefox  with the bitsacm blog every time you open the terminal.

Since a lot of modifications will be done to the .bashrc throughout this tutorial, it is important to note the following things -

  • Adding something into the .bashrc file, only takes effect after you open a new terminal. So, you would either have to run source .bashrc or open a new terminal every time.
  • Bash is picky with spaces, so make sure your spacing is appropriate.

To make the terminal more user-friendly, start by adding these lines to your .bashrc. The shopt -s command helps select options for the shell.

shopt -s cdspell        # Spell correction
shopt -s autocd         # Allows you to cd by just typing the folder-name
shopt -s dirspell       # Directory Spell Check

Take a look at what some of the above options allow you to do!

Some other options you may want to check out include expand_aliases, nocasematch or direxpand.

Next, you would want to keep an unlimited history of your commands, you could set this in your .bashrc (check .bash_profile for mac users). There should already be a line, you only need to change it to,

HISTFILESIZE=
HISTSIZE=

To find something from the history of your commands, type ctrl-r on a command. You could also see your command history in the .bash_history file.

Another cool feature about bash are aliases. These are shorter commands you could use to run commands.

e.g.- Adding alias py='python3' in the .bashrc , allows you to run python by just typing py in the command line. Feel free to make your own cool aliases although it's best not to make to many!

Adding Command Line Apps

Next, lets explore some awesome command line apps that makes the command line experience better. These apps aren't installed by default, so you would need to install them with, sudo apt install <program-name>

If you can't get the <program-name> correctly, you could search for the program's exact name with apt search <keyword>.

Note: If you are running a different OS from Ubuntu/Debian, replace apt with the equivalent command in your package manager. i.e., dnf for Fedora, pacman for Manjaro, brew for Mac.
It's always good to check out the github/gitlab pages linked in the heading if you're stuck on the installation or want to know more features!

Ranger

Ranger is a terminal-based file manager inspired by vim. To install it type, sudo apt install ranger.  
For using ranger, you could navigate the directories by using the arrow keys or h j k l (vim keys), and quit with q or :q. Type ranger in the command line and give it a try!

Now, you may wonder, whether there is a way to navigate through directories using ranger instead of writing the ugly cd command all the time. The answer, of course, is Yes! This could be done by adding a small function into your .bashrc.

Try and see if you can figure out what it does! (Hint: It saves the location of the new directory in a tempfile and 'cd's into it!)

# Use ranger-cd to automatically change directory
function ranger-cd {
    tempfile="$(mktemp -t tmp.XXXXXX)"
    ranger --choosedir="$tempfile" "${@:-$(pwd)}"
    test -f "$tempfile" &&
    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
        cd -- "$(cat "$tempfile")"
    fi
    rm -f -- "$tempfile"
}

Now, every time you run ranger-cd and navigate to the required folder and exit, you would switch to that directory.
Since ranger-cd is a long command, you can make things nice by adding an alias, alias rr='ranger-cd' into our .bashrc (after the function). This way, you can use ranger to navigate folders instead of running cd every time.

ranger-cd ('rr') to switch directories

In the screen above, the rr command being used in the 'example' directory but the terminal switches to last directory(Slack-Stock-DAG).

FASD

Ranger is good but there is a fasder way to cd into a folder.

This command-line program essentially caches every folder and file you visit. This way, every time you want to change directory into a visited folder, you just type z <folder name>, and fasd will automatically redirect you to the most frecent directory with that name i.e., a combination of frequency and recency. Note that you have to visit the directory, for being able to switch to it.

You can install fasd the same way as ranger, sudo apt install fasd. However for fasd to work, you need to add the following lines to your .bashrc.

eval "$(fasd --init auto)"
export fasd_cache="$HOME/.config/fasd/.fasd-init.bash"
if [ "$(command -v fasd)" -nt "$fasd_cache" -o ! -s "$fasd_cache" ]; then
  fasd --init posix-alias bash-hook bash-ccomp bash-ccomp-install >| "$fasd_cache"
fi
source "$fasd_cache"
unset fasd_cache

This lets it store the directories/files you visit for each command executed. Take a look at how you can use fasd, to switch directories!

Switching from the home directory to 'Slack-Stock-DAG' in one command

FZF

Fzf (for fuzzy-finder) is a great tool to find files with a neat list showing the matching files. To avoid additional steps, it's best to install fzf using git,

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

Fzf has a wide variety of features like,

  • Searching the history of your commands better. Try typing ctrl-r!
  • Completion of commands with **. Try cd ~/Documents/** and press <tab>!
Using Ctrl-r in fzf for selecting from the command history

This program can also be used to select an option from a list.
For example, try and run ls | fzf and type in a file you want to select. The command will print that file to the terminal (stdout).

Now, what if you could use the frecency list of fasd with fzf?
To combine fasd+fzf, add the following function in your .bashrc.

cwd() {
  local dir
  dir="$(fasd -Rdl "$@" | fzf --select-1 -exit-0 --no-sort --no-multi)"
  cd "${dir}" || return 1
}

Explanation: Fasd provides the list of most frecent directories to fzf. We can then choose the directory we want to switch to, this is stored in the variable dir.

Go ahead and run cwd and you should find a neat list of the directories you most "frecently" visited! Remember to cd to directories first, for them to appear on the list.

In this example, the cwd command is run with the ctrl-o keybinding (which we'll add later). The command opens fzf with the list of directories you have visited and lets you select the directory you want to switch to.

Effective Use of the 'cwd' command

It is useful to note that a lot of other things could be done with fzf, like a music player (clerk) or for selecting a website from the google chrome browsing history. You could check out more cool scripts for fzf on it's wiki.

Additional Tips: Other similar programs include, dmenu and rofi which are also pretty cool and worth checking out.

Adding Keybindings

Although the commands and aliases aren't too long, keybindings can make your workflow a lot smoother.

To add keybindings for your commands, add lines like these into your '.inputrc' file. This is another dotfile, similar to .bashrc but is used for mapping keyboard inputs. If this file doesn't exist, go ahead and make one!

# Press Ctrl-o for opening directory list
"\C-o": "cwd\n"
# You could add more keybindings like Ctrl-x for starting python!
"\C-x": "python\n"

Making the Terminal Prettier

A screenshot of a fancy terminal!

To make your terminal look better- pick a nice color scheme and try out terminal based programs like powerline, figlet, neofetch, etc..

sudo apt install neofetch figlet cowsay lolcat

After installing some of the above programs, take a look at some of these cool commands and add the ones you want into your .bashrc.

fortune | cowsay -f stegosaurus
figlet "Hello World"
figlet -f mini "$(date +'%H:%M')" | lolcat
neofetch

Other Tools and Scripts for a better ecosystem

Apart from the programs mentioned, here are some other apps you may want to checkout-

  • Tmux or Terminal-Multiplexer for spawning and tiling multiple terminals at a time.
  • Taskwarrior for a handy to-do list.
  • Calcurse for a calendar manager.
  • Vim/Neovim for the awesome text editor which inspired ranger. Check this blog to know more!

Some resources to explore more include,

Keep Exploring!