Search code examples
vimsyntax-highlighting

VIM syntax highlighting of text files


Vim used to syntax highlight plain text files(*.txt) as conf files if the first character of the file was a #. But after I updated to 8.0.3, this feature has disappeared. Is there a way to fix this other than the solution mentioned here? i.e without modifying the file.


Solution

  • function SetConfType()
      if !empty(matchstr(getline('1'), '^#\s.*'))
        set filetype=conf
      endif
    endfunction
    
    autocmd  BufEnter *.txt call SetConfType()
    

    Update:

    This oneliner does not require a function. It is a little bit more elegant.

    au BufRead * if getline(1) =~ '^#\s.*' | setlocal ft=javascript.flow | endif