Quickly run and halt Vagrant

Are you using Vagrant as your development environment?

If yes, and you are a WordPress developer, you should know that there’s a project for Vagrant and WordPress developers called VVV which is a Vagrant configuration for WordPress development.

If not, look at it, it will change the way you develop for WordPress.

Anyway, if you use it, you know that to start your local machine you have to use the command vagrant up in your terminal.

That is fast and easy, but only if you installed VVV in your root directory.
I didn’t, it’s in a folder on my Desktop, so every morning, I have to open the terminal, cd to the Vagrant directory and run it.

It still is fast honestly, but I have to type the path to the directory every morning, then cd back to the root to do other tasks. It started annoying me. So I decided to create an alias for it.

If you are using Linux or OS X maybe you already heard of aliases. They are commands create by you that do things. You define what they have to do.

For example, I created an alias to cd in the Vagrant root, run Vagrant, and cd back to my user root. So instead of opening the terminal and running cd Desktop/vvv/vagrant-local and then vagrant up now I only open the terminal and run vagup, or vaghalt to halt Vagrant.

Nice eh? Let’s see how to create this alias!

Adding the Alias to .bash_profile

To create an alias, open the terminal from any location and go to your user root:

$ cd ~

Now, you can check if the file .bash_profile already exists, but it does not really matter. If it exists, it will be opened when trying to edit it, otherwise it will be created. So open the file in your terminal:

$ nano .bash_profile

Add the aliases at the end of the file:

# Vagrant up
alias vagup='cd ~/path-to-vvv && vagrant up && cd ~'

# Vagrant halt
alias vaghalt='cd ~/path-to-vvv && vagrant halt && cd ~'

Replace /path-to-vvv with the path to your VVV installation. In example, my aliases are:

# Vagrant up
alias vagup='cd ~/Desktop/vvv/vagrant-local && vagrant up && cd ~'

# Vagrant halt
alias vaghalt='cd ~/Desktop/vvv/vagrant-local && vagrant halt && cd ~'

Use CTRL + x to close the file, it will ask you if you want to save the changes, type y and then hit Return/Enter.

You have the aliases now, but they are still not working, we need to tell the system about the changes, so run this command in the terminal:

$ source .bash_profile

Now try to run the command vagup. You will see that your Vagrant machine will start, or it will tell you that it’s already running.

More Posts That You Might Like…

Leave a Reply



Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: