Many developers spend a fair amount of time in the command line and for those of us that do, command line aliases are among the tools that are often used to make interactions a bit faster.

When developing web projects, you will need to open your project in your web browser at some point. There are various local development tools such as Laravel Valet and Vagrant that run a web server and allow you to map your project to a url in your browser. I typically use the .dev tld but you are free to use which ever you prefer.

NOTE: If you are using Valet, Valet has it own open command that will do pretty much what this script does for any project folder you have linked. The only thing the Valet's open command doesn't do is open url's directly.

Mac OS ships with a handy command called open which can, among other things, open a url in the OS' default browser.

The following is a little Bash script that wraps the open command. It accepts a project directory as an argument and opens the specified project in the browser with a .dev tld. It also will accept a URL just as you would if you were to call open directly.

Usage

  1. $ visit or $ visit . will open the current folder with a .dev tld in the default browser.
  2. $ visit /path/to/project will open the specified folder (e.g. http://project.dev) with a .dev tld in the default browser.
  3. $ visit example.com or $ visit https://example.com will open the url with the protocol specified or add http:// by default if none is specified.
  4. Add the -s flag as the first argument to the script and the protocol will be set to https://. For example visit -s /path/to/project will open https://project.dev

Show me the code!

#!/bin/bash
# Open the default browser and visit a url, visit
# the current directory, or visit a specified
# directory with a .dev tld

# set the tld for the scipt
tld=".dev"

# determine the protocol and project
if [[ $1 == '-s' ]]; then
    protocol="https://"
    project=$2
else
    protocol="http://"
    project=$1
fi

# build and open the url with the correct protocol
if [[ $# -eq 0 || $project == '' || $project == '.' ]]; then
    /usr/bin/open "$protocol$(basename `pwd`)$tld"
elif [ -d $project ]; then
    /usr/bin/open "$protocol$(basename $project)$tld"
else
    if [[ ! "$project" =~ http://* && ! "$project" =~ https://* ]]; then
        /usr/bin/open "$protocol$project"
    else
        /usr/bin/open "$project"
    fi
fi

Here’s a quick run down.

We have the option of configuring the tld at the top of the script. The first if block determines the presence of the -s flag and sets the protocol and project or url accordingly. Next we check if an argument was provided, if the project was specified or if the project specified is equal to ‘.’. If any of those conditions pass we are dealing with the current directory. Next we check if the project specified is a directory. We use the basename command to determine the paths basename which is the domain we will open. If we make it to the else statement we are dealing with a url so we check if a protocol was provided. If it was we use that protocol, if not we apply the default which in this case is ‘http://'

Simply add an alias to your profile in your shell of choice and you're all set.

alias visit="/bin/bash ~/code/scripts/bash/visit.sh"