How to edit your hosts file with Sublime Text 2
Every developer has probably had to edit their hosts file to point to a dev server at some time or another. On the mac, you would normally do this from terminal using a command line similar to:
sudo { nano | vi | other-editor } /private/etc/hosts
I’ve spent a lot of time in the shell so that doesn’t bother me much, but the initial reaction to a command line interface or shell-based text editors for some people is to run screaming from the room. I’ve finally worked out a solution for those suffering from CLI-Phobias and rather than email it out to all of them, I thought I’d put it up here and point at it instead.
Step 1: Download and Install Sublime Text 2
I say this as though you aren’t already using Sublime Text 2, but here is the link just in case something actually is wrong with you. http://www.sublimetext.com/2 Download and install it.
Step 2: Create shortcuts to the sublime command line
- Go to Finder and click Applications and then Utilities.
- Find and double-click Terminal.app
- In your terminal window paste the following:
sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/sublime - When prompted for a password, enter the password that you use to log into your computer.

Step 3: Create the EditHosts file in Sublime
- Open Sublime and paste this into line 1:
sublime -n /private/etc/hosts
- Click File and Save As
- Name the file EditHosts and save it to your Desktop
Step 4: Make the EditHosts file executable
- Go back to your terminal window (see step 2 if you’ve closed it already)
- Paste this command into your terminal window and hit enter:
cd ~/Desktop/;chmod u+x EditHosts
This command changes directory to your Desktop and makes the EditHosts file executable for the user.
You have now created a shell script named EditHosts. Normally a shellscript file will end in .sh, but that’s unnecessary in this instance and may be confusing for people who’ve never been exposed to a unix shell. When you doubleclick this file, it will run the command to start sublime with a new window with your hosts file ready for editing.
At this point, you can rename this file anything you like and it will still work.
Step 5: Configure Terminal to close on exit
If you’ve already double-clicked your EditHosts file, you’ve noticed that it leaves a terminal window behind when you’re done. This next step will tell terminal to close that window after it runs our script.
- Go to Finder and click Applications and then Utilities.
- Find and double-click Terminal.app
- Click Terminal and preferences at the top of your screen
- Click Settings tab at the top and find the “Shell” button on the right.
- Find where it says “When the shell exits:” and choose the “Close if shell exited cleanly” option.

- Click the red X at the top to close the preferences window.
Now when you double-click EditHosts, it should open a terminal window just long enough to run the sublime command and open the hosts file and then the window should close automatically on its own.
So how do we use it?
Great, so now you’ve got this icon on your desktop but what does it do? When you double-click it, you get a new Sublime Text 2 window with your hosts file. You are now ready to add entries and potentially override real DNS to point any name you like at any IP address you choose.
- Double-click EditHostsNow you have a new Sublime Text 2 window with your hosts file ready for editing.
- Add entries that make sense to you. The format is IP Address, a space or tab, and then the hostname.Here’s a sample host file:
Lines 1-9 (or 10) on your host file may be slightly different from this example. Those lines should not be touched. You will likely not have anything below line 10 yet unless you’ve already done this before from the command line.
I included lines 11-13 to demonstrate how you can override DNS for any domain and point it anywhere you like. Because I occasionally typo gmail.com as gmai.com and have no need to visit the gmai.com site, I have aliased gmai.com to the IP addresses of a couple of the servers at google.com. Now when I type gmai.com in my browser I at least end up on the google servers instead and it’s pretty quick to get from there to gmail.
The lines from 15 down all send my browser to local installations of various CMS’s. This is tremendously useful if you are utilizing Name-based Virtual Host on a local apache web server (MAMP, WAMP, XAMP). Maybe I’ll go into that in another post sometime.
- Click File and Save in Sublime Text 2
- When prompted for a password, enter the password that you use to log into your computer.
That’s it. I know it looks like a lot of work, but it’s really only a couple of quick steps. Once you’ve completed them, editing hosts files becomes as easy as editing any other text file on your computer.






