According to much online documentation (e.g.), I should be able to enable bash syntax highlighting via modelines by adding a line like:
# vim:let g:is_bash=1:set filetype=sh:
And in .vimrc
:
filetype plugin indent on
syntax on
set modeline
set modelines=4
Unfortunately, when I add those lines I receive an error message:
"packer/shunit2/include.sh" 85L, 2749C
Error detected while processing modelines:
line 85:
E518: Unknown option: let
Where line 85 is of course the modeline from above. (And it is the last line in the file.)
Why does this not work despite so many pages suggesting that it should, and what does the error message mean, and how can I change this setup so that my bash scripts open with correct bash syntax highlighting?
No, that answer is wrong. You cannot define variables in a modeline, only (certain) options can be set there (for security reasons).
To achieve bash syntax highlighting, you have the following options:
let g:is_bash = 1
in your~/.vimrc
. This is the easiest option, but only works if all you ever edit is Bash (not Korn shell or any other shell dialect).#!/bin/bash
; the$VIMRUNTIME/syntax/sh.vim
will then automatically detect it.:autocmd BufNew,BufRead {pattern} let b:is_bash = 1
will do. If you need to inspect the file contents, put the code in~/.vim/ftplugin/sh_bashdetection.vim
or so.let b:is_bash = 1
) into a.lvimrc
file in the root of that project, and it'll automatically apply the settings to all files within that subdirectory tree.