How do I enable syntax highlighting for .bash_aliases in vim?

Solution 1:

Go to vim and run:

:echo $VIMRUNTIME

Usually the value will be something like:
/usr/share/vim/vim72
Then edit (using root) the file /usr/share/vim/vim72/filetype.vim
Search for bashrc.
You will find a line that looks like this:

au BufNewFile,BufRead .bashrc*,bashrc,bash.bashrc,.bash_profile*,.bash_logout*,*.bash,*.ebuild call SetFileTypeSH("bash")


Edit the line and add your filename (.bash_aliases) to it.

That's it, now it should work.

Solution 2:

The answer is in this: vimdoc - setf but to throw you a bone, if you just want the syntax & syntax highlighting to work you can do:

  :setf bash

Another possiability which I just realized when I was answering another VIM question was that you could also add this section to your .vimrc file and it would automatically enable syntax highlighting for the .bash_aliases file everytime you edit it without needing a modeline or having to manually type in :setf bash each time you open the file.

if has("autocmd")
  augroup bashalias
    autocmd BufRead,BufNewFile .bash_aliases set filetype=bash
  augroup END
endif

Thirdly as Mugen Kenichi below in the comments points out, you could also add a modeline to the .bash_alias file also as such:

# vim: set filetype=bash: