4

In short, can single bash commands in the terminal contain conditionals? If so, how?

I have in my vimrc (shared across systems) the following command to open my current LaTeX document in .pdf form: map ,v :!gnome-open %<.pdf <CR> <CR>

My question is, what is the best way to have ,v execute simply "open %<.pdf" when I'm at home on my OS X machine? Here is my pseudo-code guess:

... if [$OSTYPE == "darwin*"] then open %<.pdf else gnome-open %<.pdf ...

This is executed as a single BASH command. Are such conditionals possible? If so, could someone help me with the syntax? If not, can this be done via conditionals in the vimrc file?

  • If you want to use a single script across all OSes, I follow Jander's suggestion since it seems like you're comfortable enough with bash. Write a bash script that's checks $OSTYPE, uname, which/whereis/type, or whatever is appropriate and call the script from vim using your keybinding. – penguin359 Apr 22 '11 at 6:44
  • @Jander and @penguin359: both of your answers have taught me what I need to know to do this and more. I'm very thankful! – user6845 Apr 23 '11 at 4:07
6

An alternate would be to just generate the map keybinding correctly for the OS. For example:

if executable("cmd.exe")
    map ,v :!cmd.exe /C start "" "%<.pdf"<CR><CR>
elseif $OSTYPE =~ "darwin.*"
    map ,v :!open '%<.pdf'<CR><CR>
elseif executable("gnome-open")
    map ,v :!gnome-open '%<.pdf'<CR><CR>
endif

This was tested and appears to work, but as my Vim script is a little shaky and I might have missed some details like proper quoting, I'd probably go more with Jander's approach and just use a shell wrapper which I am far superior in writing.

map ,v :!open.sh '%<.pdf'<CR><CR>

And in ~/bin/open.sh:

#!/bin/sh

if echo "$OSTYPE" | grep "^darwin" >/dev/null 2>&1; then
    open "$@" &
elif type gnome-open &>/dev/null; then
    gnome-open "$@" &
fi
  • Nice. I missed the idea of doing the OS detection from within the shellscript. One minor point -- I think you mean "gnome-open" instead of "gnome-terminal". – Jander Apr 25 '11 at 4:35
  • @Jander yea, I was thinking there was something funny about what I wrote. – penguin359 Apr 25 '11 at 17:45
5

Personally, if I were going to do this, I'd create a ~/bin/open.sh script with whatever "open" command is correct for the OS, and make sure ~/bin is in my path on all my machines. It scales better that way in case I want to add a Windows+Cygwin box, for example. The downside is that you need to have a different script for each type of machine, although the scripts are dead simple so it's really not too bad.

So, in ~/bin/open.sh on the Mac:

#!/bin/sh
open "$@"

And in ~/bin/open.sh on Linux:

#!/bin/sh
gnome-open "$@"

And in vimrc:

map ,v :!open.sh %<.pdf<CR><CR>

...Alternately, if you prefer to have everything on the :! line, then you can use a semicolon wherever you would normally use a newline in a shell script. To adapt your example:

... if [ "$OSTYPE" == "darwin10.0" ]; then open %<.pdf; else gnome-open %<.pdf; fi

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.