Bash Shell Tricks & Shortcuts

Bash Shell Tricks & Shortcuts

The more my web development skills evolve, I have noticed the more time I spend in a terminal window. There's something serene about a black background with green text that takes me back to my childhood when I used to play around with a friends VAX terminal. I have recently learned a few cool tricks thanks to sites like David Walsh's and Smashing Magazine.

Note, you will need to run source ~/.bash_profile after editing your bash file in order to use these commands without having to log out and back in or restarting your computer.

Trick #1: Get your internal or external IP with a simple command

David Walsh has a quick write up on how to actually do this here, but my solution takes it one step further and adds them as aliases to your bash profile script.

Add the following lines of code to your ~/.bash_profile file:

alias intip="ipconfig getifaddr en0"
alias extip="curl ipecho.net/plain ; echo"

Then simply run the commands from a terminal window: intip -or- extip and you will get your IP address.

  • The intip command may need to be tweaked if you have multiple interfaces.

Trick #2: Serve a directory up as a webpage with Python Simple Server

Here's another David Walsh tweak. Python has a cool built in function that will serve up a folder as if it were sitting behind Apache or Nginx, and while it may not serve everything perfectly (some runtime based JS doesn't always work right and there's not PHP support), it's really handy for debugging some frontend code or sharing a link to a file.

Add the following line of code to your ~/.bash_profile file:

alias pserve="python -m SimpleHTTPServer"

Then just go to the folder you want to serve and type pserve . By default it will bind to your IP address and port 8000, though you can change the port with a command flag.

Trick #3: Keyboard Shortcuts Galore!

Another thing that I have been using more and more of is keyboard shortcuts. I almost have it down to the point that I can navigate the computer entirely without a mouse. Some of the most helpful are the shell/terminal shortcuts. Here's a few of my favorite:

  • Ctrl + a & Ctrl + e These allow you to jump your cursor to the beginning of the line (A) or the end of the line (E).
  • Option + Arrow Keys This jumps you forward or backward, word-by-word. Also works pretty much anywhere you can type text.
  • Ctrl + R Allows you to do a search of your bash history. So if you have a really long command that you can't quite remember, this is an easy way to find it.
  • !! Rerun last command.

So that about wraps it up. I'm sure I will come back with a bunch of new things, so if you have any suggestions of your own, please add them below.

Related Article