Skip to content

Instantly share code, notes, and snippets.

@pluma
Last active December 16, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pluma/5510744 to your computer and use it in GitHub Desktop.
Save pluma/5510744 to your computer and use it in GitHub Desktop.
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
Copy link
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