Skip to content

Instantly share code, notes, and snippets.

@pluma pluma/vagrant_aliases.sh
Last active Dec 16, 2015

Embed
What would you like to do?
Bash functions I found useful when working with multiple vagrant projects.
_vagrant_dirnames() {
if [[ $COMP_CWORD > 1 ]]; then
COMPREPLY=()
return 1
fi
local cur="${COMP_WORDS[COMP_CWORD]}"
pushd ~/vagrant/ > /dev/null
local opts=( $cur*/ )
popd > /dev/null
if [[ "$opts" == "$cur*/" ]]; then
COMPREPLY=()
return 1
fi
COMPREPLY=( ${opts[@]%*/} )
return 0
}
# Change to a vagrant dir
v() {
local DIRNAME=$1
if [ -z $DIRNAME ]; then
cd ~/vagrant/
return
fi
if [ ! -d ~/vagrant/$DIRNAME ]; then
echo "No such vagrant dir: $DIRNAME"
fi
cd ~/vagrant/$DIRNAME
}
# Execute a vagrant command in a vagrant dir
va() {
local DIRNAME=$1
if [ ! -z $DIRNAME ]; then
if [ -d ~/vagrant/$DIRNAME ]; then
shift
pushd ~/vagrant/$DIRNAME > /dev/null
else
DIRNAME=""
fi
fi
vagrant $@
if [ ! -z $DIRNAME ]; then
popd > /dev/null
fi
}
# Execute "vagrant up" in a vagrant dir
vu() {
local DIRNAME=$1
if [ ! -z $DIRNAME ]; then
if [ ! -d ~/vagrant/$DIRNAME ]; then
echo "No such vagrant dir: $DIRNAME"
return
fi
pushd ~/vagrant/$DIRNAME > /dev/null
fi
vagrant up && vagrant ssh
if [ ! -z $DIRNAME ]; then
popd > /dev/null
fi
}
# Bash completion ftw
complete -F _vagrant_dirnames v va vu
@pluma

This comment has been minimized.

Copy link
Owner Author

pluma commented May 3, 2013

Some helpful bash functions for working with vagrant. These assume your vagrant instances live in direct subfolders of ~/vagrant/.

v [<dirname>]

cd to a vagrant dir with the given name.

va [<dirname>] <action>

Executes the given vagrant action in a vagrant dir with the given name or the current directory.

vu [<dirname>]

Calls vagrant up for a given vagrant instance (or in the current directory), then vagrant sshs into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.