3

I have a file written in bash (with the she-bang) and called "catalog", and the syntax highlighting is incorrect. If I rename the file, the colors become correct.

I have not configured anything in any file for syntax highlighting.

If I type :set syntax=sh the colors got correct. However, I do not want to do this, but instead, I want to see, each time I open the file, the correct syntax highlighting.

How can I do this?

3

Unfortunately Vim has a hard filetype detection on the name catalog. It detects it as SGML catalog file. I'm not sure if that is a wise idea.

Anyway here is a workaround. Add the following line as last line in your script:

# vim: ft=sh

Save and then reload the file with :e!. And the file should be detected as shell script. (If not, see below.)

The line you inserted, is called "modeline". Vim checks the first few and last few lines for such a modeline (5 by default). If it is found, the settings are applied. In this case it sets the filetype to sh.

A modeline could contain multiple settings, like:

# vim: ft=sh et sw=2

This sets the filetype to sh it sets expandtab and a shiftwidth of 2.

If this doesn't work, check the modeline option with

:set modeline?

If it prints nomodeline you have to adjust your vimrc.

More details about modeline can be found with :help 'modeline' and :help modeline.

The number of checked lines at start/end of the file is configured with modelines (plural). See :help 'modelines'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.