Using your Bash and Vim config on multiple computers
Not long after I switched to Linux I started modifying my .bashrc file. In a .bashrc file you can set options for Bash, add aliases, configure your Bash prompt etc. When I decided to start using Vim as my main editor a few weeks ago, I also started tinkering with the .vimrc file. The .vimrc file is, similar to the .bashrc file, the place-to-be to configure Vim.
Obviously I wanted to share all these config-files between my work machine and my personal computer. That is why I set up a Git repository on my Dreamhost webspace. With most of your important config-files in a Git repository you can easily share your config between multiple computers and you get a history of your modifications as a free bonus.
Because I want to keep it clear which files are in the Git repo and which files are not, I just created a directory in my homedirectory called 'scripts' (so at ~/scripts/). Everything in that directory is in Git, everything outside the directory is not. But because the Bash and Vim RC-files are outside the ~/scripts/ directory you will have to do some additional step to make those programs use the RC-files from the scripts-directory. For Bash, you can simply source your own Bash-file from your .bashrc by adding the following lines to the end:
if [ -f ~/scripts/bashrc/bootstrap ]; then
source ~/scripts/bashrc/bootstrap
fi
For Vim, I simply create softlinks to .vimrc and .vim/ (if .vimrc and/or .vim/ already exist you will have to remove them first). On the commandline type:
# make sure you are in your home directory
$ cd
# remove the .vimrc and .vim/ if it exists (this will delete everything, obviously!!)
$ rm .vimrc
$ rm -rf .vim/
# make the softlinks
$ ln -s scripts/vimrc .vimrc
$ ln -s scripts/vim/ .vim
Settings specific to one computer
Because I use every computer for a different task, my preferred configuration sometimes slightly differs for each machine. That's why I came up with a small addition for both Bash and Vim. In Both cases the default configuration file sources an additional configuration file, which is specific to the machine. To automate this, I use the hostname of the machine. So for Bash I have the following lines at the end of my bootstrap script:
if [ -f ~/scripts/bashrc/$(hostname -s) ]; then
source ~/scripts/bashrc/$(hostname -s)
fi
When this code is run on my personal pc, it will look to see if ~/scripts/bashrc/andoria exists, and if so execute it. On my work-laptop it will source the file ~/scripts/bashrc/delta-vega. When I use the scripts on another machine, no file specific to that host would be found, so nothing would be executed. But adding host-specific config for that machine would be as simple as just creating an additional file with the hostname as the filename.
For Vim I use a similar approach. At the end of my .vimrc I have the following code, which checks for a host-specific vimrc-addition and sources it:
let hostfile='/home/jeroen/scripts/vim/vimrc-' . hostname()
if filereadable(hostfile)
exe 'source ' . hostfile
endif
So again, I have the vimrc-andoria file for my personal machine, and vimrc-delta-vega for my work-laptop. One small note: I couldn't get this working without the full path (/home/jeroen/), instead of ~/. But that is probably just something I did wrong. After all, I still need to learn the basics to Vim-scripting ;).
Comments
Graham Ashton on 2011-05-25 12:09
Checking hostname, but ignoring domain name
Hi. I found your snippet on github and borrowed it for my vimrc. My laptop switches it's domain name as a function of which network it connects to, and `hostname()` returns the full domain name.
To drop the domain part I just applied this patch:
https://github.com/gma/dotvim/commit/5669f66ff5c0b0f554085c3d08bb9cd5470...
Cheers,
Graham
Jason Hummer on 2011-09-22 16:32
I've tried following some of
I've tried following some of the instructions below but still it won't work. Any solutions?
Comment Atom Feed