Joel Always one syllable, sometimes "@jayroh"

Correctly Syntax Highlight Your Shell Scripts

04 Jan 2016

Not all shell scripts are created equal. You may have a do-something.sh script, or maybe you call it do-something? Maybe it uses the bourne shell? Maybe bash? Maybe zsh? I don’t know. We all have opinions.

You’re probably using vim to edit that thing though, right? If you’re reading this, you probably do. Isn’t it bothersome when you’re editing that file and want it highlighted with the correct syntax? That’s easy enough, if you use the .sh extension.

au BufRead,BufNewFile *.sh set ft=sh

It’s less easy if you mix and match shells or don’t use the file extension. I sometimes don’t. In those cases I check for what’s going on in the she-bang.

au BufRead,BufNewFile,BufWrite *
      \ if getline(1) =~ "#!/bin/bash" |
      \   set ft=sh |
      \ endif

au BufRead,BufNewFile,BufWrite *
      \ if getline(1) =~ "#!/bin/zsh" |
      \   set ft=zsh |
      \ endif

Maybe that’s overkill. Have a better solution? I bet you do :). Let me know!

I'm working on something called shubox.io!

Want to know more about it? (Answer: "Yes. I do") Check it out or sign up for my newsletter to receive news, announcements and other good bits!