oooszg
Last Updated: February 25, 2016
·
5.593K
· ldavisrobeson
Twitter robeson

Vagrant tweaks to make it more like your local command line app

1/12/15 update:

You can greatly simplify this process by adding a single line to your Vagrantfile to add a shared folder. I'm keeping a "vagrant-shared" directory in Google Drive, for example.

Store your pimped out .bash_profile file there and Vagrant will automatically pull it in, meaning you can use the same file for multiple Vagrant boxes.

Here's the Vagrantfile line I added, your path(s) will differ (see Vagrant docs for more info):

config.vm.synced_folder "/path_to_your_local_shared_directory", "/home/vagrant"

Now it's only 1 step! Thanks to @just3ws's comment for giving me the idea. :)

-----

Original post:

Vagrant is great, except for the loss of personal tweaks and familiarity you're used to if you've pimped out your local command line app (Terminal/iTerm2/etc).

Here are a couple things I've started copying over to my Vagrant boxes to save a few keystrokes and add back my favorite local mods.

Copy aliases and Git-related enhancements from Bash Profile

nano /home/vagrant/.bash_profile

At the bottom of any existing content, add anything from your Bash profile that would come in handy on your Vagrant box. For example, here's what I copy over:

#########################
# Git
#########################
# Show present working directory and Git branch at prompt
# source: http://www.developerzen.com/2011/01/10/show-the-current-git-branch-in-your-command-prompt/
function parse_git_branch () {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# aliases
alias g="git"
alias ga="git add"
alias gb="git branch"
alias gc="git commit"
alias gco="git checkout"
alias gcol="git checkout live"
alias gcom="git checkout master"
alias gcos="git checkout stable"
alias gd="git diff"
alias gl="git lg"
alias gm="git merge"
alias gp="git pull --ff"
alias gpol="git push origin live"
alias gpom="git push origin master"
alias gpos="git push origin stable"
alias gs="git status"

# ANSI colors: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_GREY="\[\033[0;37m\]"
DARK_GREY="\[\033[1;30m\]"
NO_COLOUR="\[\033[0m\]"

# prompt config:
PS1="\[\033[0;31m\]VAGRANT $LIGHT_GREY\w$PURPLE\$(parse_git_branch)$NO_COLOUR\$ "

#########################
# Drush commands
#########################
alias d="drush"
alias dca="drush cc all"
alias dcssagg-on="vset preprocess_css 1 --yes"
alias dcssagg-off="vset preprocess_css 0 --yes"


#########################
# Misc
#########################
alias ls="ls -l"

#########################
# CD BACKSLASH SHORTHAND
#########################
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'

# Go to public directory by default
# Go to the web root directory after logging in (note: your path may be different!):
cd /vagrant/public

Exit: control + X

Save: Y and Return

Load your new settings: source /home/vagrant/.bash_profile

Voila!

Picture

Say Thanks
Respond

2 Responses
Add your response

17487
Avatar

Great tips. Another idea that just occurred to me. I'm pretty sure that the shared folders don't need to be on the same path as the VM. I think it might be possible to have a single directory that is shared between multiple Vagrant instances that holds your personalized configuration settings. Then when you're on the Vagrant instance you can use the stow tool to expand and automatically symlink the rc files. I've done a similar set up before but using a public Git repo that I exploded in the Vagrant and then linked using stow. Ultimately I've ended up working outside of Vagrant because I got tired of shuffling around the configurations and keeping them in sync. So I run the app on the host machine but only run the dependencies inside Vagrant. Your article got me thinking about maybe giving running fully inside Vagrant (or Docker) another shot.

over 1 year ago ·
18306
Twitter robeson

Thanks for reminding me about the shared folder option! I added this to the Vagrantfile so it automatically pulls in the .bashrc and .bash_profile and I can use the same files for multiple Vagrant boxes:

config.vm.synced_folder "/path_to_your_local_shared_directory", "/home/vagrant"

Now it's only 1 step. :)

over 1 year ago ·
Filed Under