Ubuntu Like Terminal in Mac | Bash
If you shifted from ubuntu to MAC recently you will that the terminal is not as impressing as it was in Ubuntu. As for someone who spends a lot of time on terminal I wanted to make my mac terminal as close to Ubuntu’s default terminal. I like to keep things easy and simple
There are two routes you can go from here:
- Use Zsh(with oh-my-zsh) and various other packages
This is what you will find is the most common way to go about powering up your terminal. I realized I didn’t need zsh superpowers and it was slowing the terminal down - Upgrade and customize your BASH
This option suited my need so let’s go with this one.
Here’s a list of things I need in my terminal:
- It needs to be fast (no compromises here)
- Appropriate colors for files and folders etc
- Command completion (Because I am dumb)
- Some nice message like : ‘we-are-ready-to-take-down-the-internet-just-press-y’
So let’s get started
Install Brew
Brew is the package manager for mac, you are gonna use it a lot in the future. Just paste the command below in your terminal and we are good to go:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Bash
Now mac comes with a default bash which is really old(3) so we are gonna install a new one. Check this blog to get more details
brew install bash
Now to set it as default shell
chsh -s /usr/local/bin/bash
Check if everything is working fine:
echo $BASH_VERSION
# 5.0.0(1)-release
Now comes the interesting part of powering your default shell. As you may notice it’s a little faster than zsh but that’s also because it’s bare bone.Now let’s prettify it.
Colors and git support
Create a file in your home directory
touch ~/.bash_profile
Now to add colors into this file.
# for Brew
export PATH="/usr/local/bin:$PATH"# Hides the default login message
export BASH_SILENCE_DEPRECATION_WARNING=1# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac# Enable colors in bash
export CLICOLOR=1
export LSCOLORS=GxBxCxDxexegedabagaced
# get current git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}# find username@hostname:$
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;32m\]\$(parse_git_branch)\[\033[00m\]\$ "
Tip: You can copy the content from here and use pbpaste >> .bash_profile
to paste directly from the terminal
Tip: You can restart your terminal to see the changes or source .bash_profile
to see the changes without quitting.
Tip: You can change a lot of things from terminal preferences in Mac like colors, transparency, background etc.
Command completion
Now let’s get to the part that really boosts your productivity. Auto command completion is probably the best thing that happened since some invented peanut butter.
brew install bash-completion@2
Now it might clash with bash-completion
so just remove that package, it’s for older versions of bash
Now let’s add more content to our .bash_profile
# for bash completion
export BASH_COMPLETION_COMPAT_DIR="$(brew --prefix)/etc/bash_completion.d"
[[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh"
You can install more bash completion
packages by finding using them
brew search completion
e.g.
brew install docker-completion
And that’s all.
# try git [tab]
add branch clean difftool grep merge range-diff request-pull shortlog submoduleam bundle clone fetch gui mergetool rebase reset show switchapply checkout commit format-patch help mv reflog restore show-branch tagarchive cherry config fsck init notes remote revert stage whatchangedbisect cherry-pick describe gc instaweb pull repack rm stash worktreeblame citool diff gitk log push replace send-email status
And there you got it, folks!!
Note: Install git
using brew
by
brew install git
by default, it uses from Xcode
and I don’t know what’s up with that
BONUS: Setting up NVM
Now I am a web developer and node
is an essential part of my dev setup. All this started with nvm
being too slow on my terminal on startup and I wanted the old
bash back. So let’s get to it.
Install
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
Adding to your terminal
# for node
export NVM_DIR="/Users/jashan/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && source "$NVM_DIR/bash_completion" # This loads nvm
# bash completion for npm
source <(npm completion)
This works but it slows down your terminal considerably. Check this issue. Now until they fix we can lazy load the node using the below script. Source
# Defer initialization of nvm until nvm, node or a node-dependent command is
# run. Ensure this block is only run once if .bashrc gets sourced multiple times
# by checking whether __init_nvm is a function.
if [ -s "$HOME/.nvm/nvm.sh" ] && [ ! "$(type -t __init_nvm)" = function ]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
declare -a __node_commands=('nvm' 'node' 'npm' 'yarn' 'gulp' 'grunt' 'webpack')
function __init_nvm() {
for i in "${__node_commands[@]}"; do unalias $i; done
. "$NVM_DIR"/nvm.sh
unset __node_commands
unset -f __init_nvm
}
for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done
fi
Note: Choose only one from the above.
This ensures that nvm
only runs when we need it and not at the start of terminal.
Now you have got a productive bash to work with. It’s got colors and command completion and some more things that I don’t know of.
Sources:
https://brew.sh/
https://www.growingwiththeweb.com/2018/01/slow-nvm-init.html
https://github.com/nvm-sh/nvm/issues/1277
https://formulae.brew.sh/formula/bash-completion@2
https://misc.flogisoft.com/bash/tip_colors_and_formatting