The first thing any new developer should do is set up their bash profile with tons of awesome shortcuts and tricks to make working in Terminal faster and easier. There are useful community-driven sites and packages to automate a lot of this (for example: Bash-It, which copies over a lot from the popular ZSH framework, Oh-My-Zsh), but I always think it’s good, at least at first, to learn some basics yourself.
If you truly understand what you can do in Terminal, then you will know if you need more advanced packages or shell environments. Some basic tips and tricks for the Terminal include changing the command line to present more detailed info about your current environment, adding in aliases to common or complex commands to save time, and then learning some bash scripting to write functions to handle actions that are too complex to be handled by simple aliases.
All of these changes will be occurring in your ~/.bash_profile, ~/.bashrc, or whatever you have set up as your terminal config file.
Informative Prompts
The default terminal prompt in OSX is <ComputerName>:<CurrentDirectory> <user>$
. That’s hardly useful. How often do you need to know your computer name? And how useful is just knowing your current directory, but not any of the parent directories? The good news is, this can be modified very easily. And with all sorts of useful info.
GitHub Integration
The most useful is going to be GitHub integration. Assuming you’re using Homebrew to install packages (and if you’re not, you should be), you first need to run brew install git bash-completion
. This will install auto completion tools for git from your command line.
Then, you can modify your command prompt by adding __git_ps1
to it. This is a command that can output all sorts of info about your current git repo. By default, it will show your current branch name, but you can view all the possible options to improve it here.
Color Coded
You can also modify the colors of different parts of your command line. It looks scary, but once you get the syntax down, it’s not too bad. You can find a good list of available color commands here.
There’s also tons of info you can add to your command prompt, like full working directory, current date and time, etc.
Let’s get down to business…
So let’s put all this into practice. My current command prompt looks like this:
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\e[01;34m\]\w\[\e[00m\] `[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "\[\e[31m\]" || echo "\[\e[32m\]"`$(__git_ps1)\[\e[00m\] \n\[\e[00;33m\]\u\[\e[01;37m\] \[\e[1;36m\]∴ \[\e[00m\]'
First, I set GIT_PS1_SHOWDIRTYSTATE
to true. This means when a current branch has been modified, it will show an asterisk next to the branch name, and if the files have been added, but not committed, it will show a +.
Next, the first part of my command line is setting the color to blue (\[\e[01;34m\]
), and outputting my full current directory path (\w
).
Then, I use a little bash scripting to check if my current git status is clean, and if it is, I set the color to green, and if it isnt, I set the color to red:
`[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "\[\e[31m\]" || echo "\[\e[32m\]"`
This is then followed by the __git_ps1
call, a line break (which turns my prompt into two parts: an informational header, and the actual prompt), my username (\u
), and instead of the $, I used what I like to think of as the tri-force as my prompt symbol (∴
).
(And the few instances of [\e[00m\]
are just resetting the line color to the default.)
Here’s what this looks like in actual use:
Aliases
Aliases are just shortcuts, and are a great way to save a few seconds on typing out common commands. Here are a few of my favorites:
GitHub
alias g="git"
alias gs=”g status” alias gst=”gs” alias gco=’g checkout’ alias gcm=”gco master” alias gfo=”g fetch origin” alias gtfo=”gfo && g merge origin/develop”
These should all be pretty self-explanatory. And since these are just shortcuts, you can still type extra arguments after, like gco -b new-branch
is just calling git checkout -b new-branch
It may seem a little dumb or gratuitous at first, but once you get used to the shortcuts, you learn to love them.
Rails
alias b="bundle"
alias be=”bundle exec” alias bers=”be rails s” alias berc=”be rails c”
Again, these are simple, but you can convert any command into a shorter alias, which can then be chained or combined with other aliases.
Shortcuts
alias ...='../..'
alias l='ls'
alias ll='ls -al'
alias lh='ls -Alh'
Yes, I use an alias to make cd ../..
even shorter. Don’t judge me.
Don’t be BASHful
Another useful tool is bash scripting. It lets you write scripts that connect things together. If you want to go off the deep end into bash scripting, you can check out the Apple Developer docs on shell scripting. Otherwise, here are two tiny scripts I use to make switching between multiple projects easier.
All of my work for Revelry is in ~/dev/Revelry/
, so instead of changing directory to the full path every time, I set up a bash function called revelry_cd that attempts to cd into whatever text I type after the command ($1
refers to the first argument after the command). I also set up an alias for the function.
revelry_cd() {
cd ~/dev/Revelry/$1
}
alias cdrev=revelry_cd
This makes switching into new directories as easy as typing cdrev Project1
. But what if you have multiple terminal tabs open to multiple projects? You can set the title that shows up in the terminal tab with shell scripting ($@
is different from $1
in that it will grab all the text after the command, as opposed to just the first argument string).
set_terminal_title() {
if [[ -z $@ ]]
then
TERMINAL_TITLE=$PWD
else
TERMINAL_TITLE=$@
fi
}
alias stt='set_terminal_title'
This can then be combined with cdrev to automatically change your terminal title when you change directory. Every tab in your terminal will have a descriptive title, so you never get lost about what each tab is doing.
You can either chain the calls together in an alias like this:
alias revtest='cdrev test && stt Revelry - Test'
Or you can modify the revelry_cd() function like this:
revelry_cd() {
cd ~/dev/Revelry/$1 && stt $1
}
This way, any time I cd into a directory, it sets the terminal tab title to that directory as well.
Conclusion
These tips may seem trivial, and some of them downright silly, but once you get used to shortcuts and aliases, and creating your own time-saving shortcuts, they just just become a part of how you work. And once they become a habit, you’ll never want to go back to the slow, burdensome tedium of typing out full commands ever again.
We're building an AI-powered Product Operations Cloud, leveraging AI in almost every aspect of the software delivery lifecycle. Want to test drive it with us? Join the ProdOps party at ProdOps.ai.