Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim inconsistently syntax highlighting bash files

When I open some bash script files with vim it sometimes identifies them as conf files, that's okay, I can just correct that by setting the filetype to sh with :setf sh.

That great, except I've noticed that this doesn't fix things entirely:

Two files side-by-side with different highlighting

Notice that shopt is properly highlighted on the left, but not on the right, where I manually set the filetype to sh.

This means that when a file is identified as bash or sh by vim, it sets the filetype to sh but then does some extra steps that I'm not doing when I set the filetype manually.

Any one know what that might be, and how I could fix it?

like image 364
Peter Coulton Avatar asked Sep 16 '11 21:09

Peter Coulton


3 Answers

vim already recognizes many file types by default. Most of them work by file extensions, but in a case like this, vim will also analyze the content of the file to guess the correct type.

vim sets the filetype for specific file names like .bashrc, .tcshrc, etc. automatically. But a file with a .sh extension will be recognized as either csh, ksh or bash script. To determine what kind of script this is exactly, vim reads the first line of the file to look at the #! line.

If the first line contains the word bash, the file is identified as a bash script. Usually you see #!/bin/bash if the script is meant to be executed directly, for some other shell configuration file you should use the file extensions .bash.

The help in vim explains this as well at :help ft-bash-syntax. You can also use let g:is_bash=1 in your .vimrc to make bash syntax highlighting the default for all files with filetype=sh. If you want to look at the details, this is implemented in $VIMRUNTIME/filetype.vim.

like image 191
raimue Avatar answered Nov 13 '22 10:11

raimue


It turns out that syntax/sh.vim includes specific highlighting for Korn, Bash and sh, you just have to tell it which you're using. This is done with b:is_kornshell, b:is_bash and b:is_sh respectively.

Depending on the situation I figure I'll use the following:

ftdetect/bash.vim:

au BufRead,BufNewFile *bash* let g:is_bash=1
au BufRead,BufNewFile *bash* setf sh

Modeline:

# vim:let g:is_bash=1:set filetype=sh:

Key Mapping

nmap <silent> <leader>b :let g:is_bash=1<cr> :setf sh<cr> 
like image 35
Peter Coulton Avatar answered Nov 13 '22 09:11

Peter Coulton


Similar to Peter Coulton's solution and documented as well as an alternative in the section "new-filetype" of the "filetype" Vim help the ~/.vim/filetype.vim file could contain the following code:

if exists("did_load_filetypes")
  finish
endif

augroup filetypedetect
  au! BufRead,BufNewFile *bash* let b:is_bash = 1 | setfiletype sh
augroup END

This approach has the following implications:

  1. There is one ~/.vim/filetype.vim file instead of one for each file type under the ~/.vim/ftdetect directory.
  2. The b:is_bash variable is set local to the buffer as opposed to global by referring to it as g:is_bash.
like image 4
Tim Friske Avatar answered Nov 13 '22 11:11

Tim Friske