Last active
December 16, 2015 22:59
-
-
Save pluma/5510744 to your computer and use it in GitHub Desktop.
Bash functions I found useful when working with multiple vagrant projects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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), thenvagrant ssh
s into it