Improving your Terminal-fu

Terminal plays a huge part in doing any sort of web development on a mac or linux-based environment. Whether you are using package managers, custom build processes, or just using it for minor things here and there, it could seem like a necessary annoyance. Fortunately, there are plenty of ways you can increase your productivity and improve your terminal experience.

Update - 3/3/14

Check out part two of this series, covering ZSH.

Bash Profile

NOTE: I am not an OS developer so I don’t know if this is the best way to approach the problem, only that it has worked for me so far.

The first and most important step is to start editing your bash profile. This file is loaded whenever you start a new terminal instance (tab or window) and can fill your environment with useful shortcuts. You can customize your prompt, add in custom functions, and include aliases for common commands. In order to edit your bash profile, you need to run the following command in terminal (I use sublime text 3 editor in terminal): subl ~/.bash_profile. Of course, you can use whatever editor you have set up. This is where you’ll find path information, which is used by terminal to know where your binaries are, as well as your custom terminal settings.

Customizing the Prompt

This is pretty weird to set up, but can definitely help control your terminal experience, especially if you install a custom theme. It involves adding a custom value for PS1. There are generators online that help build your prompt more easily, such as $PS1 Generator and Bashrc Generator. In the end, both will give you a value that looks something like: export PS1="[\u@\h \W]\\$ ". Just paste that into the end of your bash profile.

Reloading Changes

Once you’ve made and saved changes to your bash profile, you need to start a new instance of the terminal. You can either create a new window or a new tab, or, if you want to just refresh the terminal inside your current instance, just run . ~/.bash_profile, which will reload the instance of the terminal.

Aliases

These are extremely handy and pretty powerful. Essentially you can store your own shortcuts to any commands you want. Here are some of mine:

123456
alias bashreload=". ~/.bash_profile"
alias bashprofile='subl ~/.bash_profile'
alias ..='cd ..'
alias ...='cd ../..'
alias ls='ls -GFh'
alias nodeproj='cd ~/Projects/node/'

You can also make use of bash scripting, which I’m not really familiar with, but I did find this super helpful command. It allows you to create a directory and immediately cd into it: md () { mkdir -p "$@" && cd "$@"; } Just paste that into your bash profile, then you can type $ md myDirectory and it will also cd into the new directory.

Other Tips

These are just some ways to improve your terminal experience. I’ve also found an additional tip that has changed my life. Have you ever been working in terminal, typed in a crazy command, run it, only to find out you needed sudo? Have no fear! Make use of the !! command. In terminal, the !! is a shortcut for whatever your last command was. So let’s say you do something like $ git pull, which errors out saying permission denied. You can just simply run $ sudo !! and it will run $ sudo git pull!

You can also use the open command to open a path in finder. Try $ open /path/to/folder to open a specific path, or $ open . to open the current path.

Conclusion

Terminal is a really powerful tool and I find that it integrates really well with any workflow. These tips really make it more fun to use! If you have any links or other tips to share, please do so in the comments.

githublinkedin