I am editing a non-standard file of Vim settings and the file has no syntax highlighting because Vim is not aware that the file contains settngs for itself.

eg - These lines are not highlighted by Vim because it doesn't know that they are vim settings although the man .vimrc file is syntax-highlighted appropriately

" Highlight trailing spaces so/4617059
:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$/

Is there some way to let Vim be aware that the file has Vim settings?

  • Could you expand your question with an example? Thanks. Apr 26 '17 at 22:01
  • 1
    :help 'filetype'. In your case set ft=vim
    – Tumbler41
    Apr 26 '17 at 22:01
  • @Tumbler41 set ft=vim seems to wokr only during that session. How can it be made to persist between sessions.
    – vfclists
    Apr 26 '17 at 22:13
  • 1
    @vfclists put it in a modeline.
    – Tumbler41
    Apr 26 '17 at 22:28
  • @Tumbler41 In fact I was looking for that word. I couldn't remember it. I take it that that modelines have to be in comments of the language, ie modelines for files of vim settings must begin with "? How do you get colored text in comments?
    – vfclists
    Apr 27 '17 at 5:49

First, you need to enable syntax highlighting and filetype plugins. Ensure that you have these lines in your vimrc file:

syntax on
filetype plugin on

With that, it should suffice to use the .vim extension in the filename, e.g. extra_settings.vim. Alternatively, you can name the file something that contains the word vimrc, e.g. extra_vimrc.

If you have modeline enabled, then you can also use a comment at the top or bottom of the file:

" vim: ft=vim

See :h modeline. Note that there are two forms. The one above is of the first form, which should not contain the keyword set.implied to be setable options.

  • I have " vim: set ft=vim at the bottom of the file, modeline is true and modelines=5 and it still doesn't work.
    – vfclists
    Apr 27 '17 at 7:21
  • You're missing : at the end, @Karl as well ;) " vim: set ft=vim:
    – grodzik
    Apr 27 '17 at 7:22
  • 4
    There are two forms of modelines (see :h modeline). The first form does not require : at the end. The second form does. However, I notice I mixed the two, since set should not be included in the first form. I'm updating the answer now. Apr 27 '17 at 7:31

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.