3

So in my project there are many bash script files that are sourced, but never run directly, so they get no shebang line and no execute bit set. Vim colors them a little bit, but doesn't use the full colorization. How do I tweak vim to give me all the normal bash colors for these files?

EDIT:

Without shebang:

Without shebang

With shebang:

With shebang

EDIT 2:

There is an answer that works for file-by-file changes below, and I'll go with that if that's all I can get, but what I'd really like is to modify a config file or something else in my vim installation so that I always get the full "with shebang" colors even when there is no shebang. There must be a file somewhere that defines the incomplete colorization, which I can just replace with the file defining the complete colorization.

EDIT 3:

The vim global variables set are not substantially different, as seen in these images (output of :let g:):

Environments

Diffed

I'm sort of at a loss here.

EDIT 4:

I dumped the entire environment from a properly-colored window (left) and an improperly-colored window (right), and diffed them, finding this:

60 b:current_syntax       bash   |   61 b:current_syntax       conf

So, for some reason it thinks my shebangless source files are conf files. So I need to figure out how to match them to bash instead.

4
  • :syntax on have you tried just turning on syntax highlighting? You could add this to your .vimrc to make it run every time you vi.
    – Matt Clark
    Commented Jun 11, 2015 at 4:30
  • @MattClark Syntax highlighting is on. What happens is, when I open a bash file with no shebang line at the top, I get incomplete colorization. See my edit for images showing the issue. Commented Jun 11, 2015 at 15:23
  • What platform are you using? Looks like windows; is it native, cygwin/mintty, or something else? Thanks!
    – cxw
    Commented Jun 11, 2015 at 16:02
  • I'm using Crunchbang, which for most purposes is Debian. The vim is 7.3, and my shell is terminator, with 256 color support enabled. Commented Jun 11, 2015 at 16:05

2 Answers 2

3

Run :setf sh

You may want to place this at the top of the files (if you want no shebang):

# vim:ft=sh
6
  • Interestingly, :setf bash removes all color from the editor on my system, while :setf sh gives the desired colorization. But I'm really wondering if there is a more permanent way to modify my vim install to use the :setf sh exclusively for all shell scripts? Commented Jun 11, 2015 at 1:32
  • 2
    The command "setf" cannot be in a modeline. All modelines can do is set options. You want: # vim:ft=bash
    – Heptite
    Commented Jun 11, 2015 at 2:02
  • 1
    There is no bash filetype. The filetype should be sh. If you need to tune the highlighting for bash, put let g:is_bash = 1 in your ~/.vimrc.
    – garyjohn
    Commented Jun 11, 2015 at 4:06
  • Marked as answer, because I couldn't figure out how to make filetype.vim select sh instead of conf for shebangless shell script files. Thanks! Commented Jun 11, 2015 at 16:55
  • So (of course) it was still a bit of a struggle ;) Debian turns off modeline by default - running :verbose set modeline? will tell you what's going on with that. Either remove the set nomodeline from /usr/share/vim/vim73/debian.vim or add set modeline to your own vimrc. Then the above modeline will work. Commented Jun 11, 2015 at 17:10
0

Put

let g:is_bash      = 1

in your .vimrc. That sets the default filetype for unknown shell scripts to bash. See full explanation in :help ft-bash-syntax

For those curious about the internals, at least on my Vim 7.4 install, g:is_bash is tested in an autocommand in $VIM/vim74/filetype.vim, which sets b:is_bash accordingly. The syntax file $VIM/vim74/syntax/sh.vim then tests b:is_bash to decide how to highlight the file.

6
  • Didn't work. I also tried adding it to /usr/share/vim/vim73/syntax/sh.vim, but no dice there either. Commented Jun 11, 2015 at 15:49
  • In /usr/share/vim/vim73/filetype.vim there is a function called SetFileTypeSH at line 1760, I think I may need to modify this but vimscript gives me headaches. Commented Jun 11, 2015 at 15:53
  • Would you be willing to try upgrading to Vim 7.4 (latest) and see if it helps? I don't have a 7.3 install any more.
    – cxw
    Commented Jun 11, 2015 at 16:03
  • Well, sometime this year I need to upgrade the whole OS, which won't be fun since Crunchbang died. I appreciate your help, but I'd rather not install a different vim just for this. Commented Jun 11, 2015 at 16:10
  • That's fair. For what it's worth, stackoverflow.com/questions/11044378 suggests others have had problems. build.opensuse.org/package/view_file/openSUSE:Factory/vim/… is a patch to sh.vim that might help. Good luck!
    – cxw
    Commented Jun 11, 2015 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .