You need to add cd /vagrant
to your .bashrc in the vm. The best way to do this is in your provisioner script.
If you don't have a provisioner script, make one by adding this line to your Vagrantfile before end
:
config.vm.provision "shell", path: "scripts/vagrant/provisioner.sh", privileged: false
Path is relative to the project root where the Vagrantfile is, and privileged depends on your project and what else is in your provisioner script which might need to be privileged. I use priveleged false and sudo explicitly when necessary.
And in the provisioner script:
if ! grep -q "cd /vagrant" ~/.bashrc ; then
echo "cd /vagrant" >> ~/.bashrc
fi
This will add cd /vagrant
to .bashrc, but only if it isn't there already. This is useful if you reprovision, as it will prevent your .bashrc from getting cluttered.
Some answers mention a conflict with .bash_profile. If the above code doesn't work, you can try the same line with .bash_profile
or .profile
instead of .bashrc
. However, I've been using vagrant with ubuntu guests. My Laravel/homestead box based on Ubuntu has a .bash_profile
and a .profile
but having cd /vagrant
in .bashrc
did work for me when using vagrant ssh
without changing or deleting the other files.