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

19

11

I'm working on reorganizing my .bashrc. I moved my aliases to .bash_aliases (which is sourced by .bashrc, but I can't figure out how to enable syntax highlighting for this file. vim seems unable to figure out what language the file is in. It works fine for .bashrc. Any ideas?

Matthew

Posted 2010-08-20T17:21:14.967

Reputation: 11 686

Answers

12

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.

Iftach Bar

Posted 2010-08-20T17:21:14.967

Reputation: 144

Perfect! This solved my problem. – Matthew – 2010-10-07T19:39:56.523

5You shouldn't edit the vim distribution (files in /usr/share/vim). Your changes may be overwritten when you update vim. Instead, you should create your own script that does the same (or adding that line to your vimrc would do it). – idbrii – 2011-09-09T19:13:00.240

11

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: 

Pharaun

Posted 2010-08-20T17:21:14.967

Reputation: 360

and/or add a modeline to your bash file http://vimdoc.sourceforge.net/htmldoc/options.html#modeline

– matthias krull – 2010-08-20T19:17:12.397

@Mugen Kenichi - Oh good catch there! I never use modeline but I've seen it in files before. – Pharaun – 2010-08-20T19:43:52.350

3This really helped me a lot, thanks guys. One thing I had to do, was I had to set the filetype to sh instead of bash, but I should probably just find a good bash vim file instead, as that would solve my problem as well :p – icco – 2010-09-28T17:44:53.713

Despite the fact I think the .vimrc edit is more reliable, VIM modelines are just awesome! Thanks. – 4wk_ – 2018-05-09T14:17:15.190

My VIM 7.3 from cygwin also does not know about "filetype=bash". If you read through filetype.vim, "sh" is used as filetype for all other bash patterns. – Bogdan Calmac – 2013-10-31T18:42:45.990

1

Add the following line to ~/.vimrc (create it if it does not exist).

au BufNewFile,BufRead .bash_aliases call SetFileTypeSH("bash")

Rudger

Posted 2010-08-20T17:21:14.967

Reputation: 219

0

Follow up on @Pharaun's answer for vim 8 and multiple alias files. In order to get the highlighting correct there were two settings I had to do. First in the new .bash_aliases file:

# vim: set filetype=bash

As well as the check in my .vimrc

if has("autocmd")
  augroup bashalias
    autocmd BufRead,BufNewFile *_aliases set filetype=sh
  augroup END
endif

Notice the filetype is sh as well as the * wildcard for multiple filenames.

Not sure why both these settings are needed. I tried to use just one setting but both combos were needed for the highlighting to be correct.

Shadoath

Posted 2010-08-20T17:21:14.967

Reputation: 197

I use vim 8 but I don't need the modeline (# vim ...). Just set the filetype to sh in my vimrc does the trick. Somehow the filetype should not bash but just sh. – Jinghao Shi – 2018-05-15T00:11:47.373