Vim syntax highlighting disappeared for text files (.txt)

5,061

Solution 1

I don't think that your Vim is broken. Try putting a hash mark (#) at the beginning of the first line in the file. When Vim sees this, it sets the filetype to conf (for a configuration file). As far as I know, Vim does not syntax color vanilla text files.

Solution 2

Vim supports modelines. To force coloring the file as conf (erichui's answer), put the following line at the end or beginning of your file:

# vim: syntax=conf

This sets vim's internal variable syntax to conf.

Note: you could also set filetype (ft) instead of syntax.

Share:
5,061

Related videos on Youtube

Vim user
Author by

Vim user

Updated on September 18, 2022

Comments

  • Vim user
    Vim user over 1 year

    My to-do list is a plain text .txt file that I load in Vim. In .txt files Vim has always coloured hash marks (#) and the text following them on the same line, which I use to mark out urgent to-do items. The other day this highlighting disappeared and I can't for the life of me see how to restore it. I've turned on all the syntax and filetype commands I can find. Should I have a text.vim or txt.vim syntax file that I might've deleted without realising? It feels like there should be a simple solution, but I can't find anything approaching a simple solution from googling round - I'm certainly not going to reinstall, write a custom syntax file, use a complex script, etc., I just want the default behaviour back.

    • Benjamin Bannier
      Benjamin Bannier over 13 years
      What platform are you running on? Windows, Mac OS or Linux (which distribution)?
  • Benjamin Bannier
    Benjamin Bannier over 13 years
    The straight forward way to force a filetype is a modeline, e.g. # vim: set filetype=conf :. Also, asciidoc comes with syntax highlighting for .txt files (but no, hashed lines are not colored specially).
  • Vim user
    Vim user over 13 years
    Wow! erichui and honk, that did the trick - I set filetype to Generic config, saved and quit and all is well again in todo-land. Thank you!
  • Scz
    Scz about 8 years
    For me, this did not work. I had to use modelines (see my answer). However, thanks for leading me to conf which, unlinke config, does not highlight numbers and some keywords.
  • Jean
    Jean over 5 years
    # in the beginning text file is not working(i.e syntax highlight as conf) in gvim anymore after I updated to 8.0.3
  • dr0i
    dr0i about 4 years
    Put it into the vimrcto enable this for all txt files: echo "autocmd BufRead,BufNewFile *.txt set syntax=conf" >> ~/.vimrc