visit
Take, for example, a common workflow that I tend to follow: completely burning a running Vagrant box to the ground. Generally, this involves executing a
vagrant destroy
followed by removing the .vagrant
directory from the project. Instead of running both of these commands every time, I can instead create an alias called vagrant eradicate
that does all that sweet, sweet destruction on my behalf.Defined using a standard
key = value
format, Vagrant aliases work the same way as Git aliases. By adding these aliases to the Vagrant alias file (which can be found either at VAGRANT_HOME/aliases
or in a custom file defined using the VAGRANT_ALIAS_FILE
environment variable), you can start crafting your own aliases almost immediately.Let’s say that we want to implement the
vagrant eradicate
command example from above. This can be done by dropping the following line into your Vagrant aliases file:eradicate = !vagrant destroy && rm -rf .vagrant
Internal Aliases
Internal command aliases call Vagrant’s
CLI
class directly. This allows you to alias one Vagrant command to another Vagrant command. This technique can be very useful for creating simple commands that you think should exist. For example, if vagrant stop
feels more intuitive than vagrant halt
, the following alias definitions would make that change possible:stop = halt
stop = halt -f
vagrant stop
vagrant halt -f
External Aliases
While internal aliases can be used to define more intuitive Vagrant commands, external command aliases are used to define Vagrant commands with brand new functionality. Similarly to Git aliases, these aliases are prefixed with the
!
character, which tells the command interpreter that the alias should be executed as a shell command.As an example, let’s say that you want to be able to view the processor and memory utilization of the active project’s running Vagrant box. To do this, you could define a vagrant stats command that returns the required information in an easy-to-read format, like so:stats = !ps aux | grep "[V]BoxHeadless" | grep $(cat .vagrant/machines/default/virtualbox/id) | awk '{ printf("CPU: %.02f%%, Memory: %.02f%%", $3, $4) }'
CPU: 4.20%, Memory: 11.00%
Previously published at