I use git to share my vimrc across multiple machines. But now I have some commands which I only want on one machine. Namely, this line: au! redhat BufReadPost (derived from this very helpful answer! https://unix.stackexchange.com/questions/59486/disable-vi-from-going-to-the-last-visited-line-upon-file-opening)

Is there a way to load this line only for my linux machine? Or maybe put into a second vimrc file which only exists/loads on linux? Or maybe another solution, so that I can keep using version control for the rest of my files but let this one command apply just for this one machine?


You can surround the config/code in question with a conditional based on an environment variable. One good candidate is envvar HOSTNAME which is likely to be universally available and (locally) unique.

For the example you mentioned we'd just need to add something like...

 if $HOSTNAME == "foo"
     au! redhat BufReadPost ...

Of course, we can also extend this to multiple hosts and/or have default settings...

 if $HOSTNAME == "foo"
     " this config
 elseif $HOSTNAME == "bar"
     " that config
     " default config

Before you set it up check availability with :echo $HOSTNAME.

If you want to look for alternatives you could run :!printenv or :!env or whatever your system's equivalent command is. In the unlikely event that you can't find an existing envvar that will work then make something up! Choose a variable name not currently in use and on the machine(s) you want to isolate put (assuming *nix) export MYSPECIALVAR=uniquevalue in your .profile, .bash_profile, .bashrc or whatever makes the most sense for your environment.

  • 2
    Works perfectly, thank you so much! – Simon Alford Jul 3 '19 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.