2

I am using Linux Mint 13 Maya Cinnamon 64-bit. My Vim version is 7.3 and I installed the latest version of Janus.

I found that for any files with a hash "#" in its content, the syntax highlight for the file does not work. For example,

# test

print "Hello"

The 'print' has color while I am editing the file. But when I save it and open it again the whole file loses syntax highlight.

If I deleted the first line and save, the syntax highlight comes back after I open it again.

This applies to all kinds of files such as .py, .c and .h. If there is a hash "#" character in the file, syntax highlighting does not work.

I have already tried "syntax on" but nothing changes.

Jeremy
  • 4,797
  • 1
  • 18
  • 26
  • What is the original (expected), and what the wrong filetype: `:setl filetype?` – Ingo Karkat Nov 05 '12 at 08:21
  • Ah, so it runs into the _general configuration_ fallback in `filetype.vim`. That should only happen when no other filetype (like Python, according to the file extension) has been set. I don't see the problem without Janus, it may be that the distribution is messing with the correct filetype detection order. – Ingo Karkat Nov 05 '12 at 09:09
  • does it mean that the filetype is parsed from the file instead of extension? – Jeremy Nov 05 '12 at 09:39
  • Yes, Vim does both matches on file path / name / extension, and can inspect the (first and last) lines, e.g. to match a `#!...` shebang line. But when a filetype has been found by extension (e.g. `*.py`), the fallback shouldn't activate. With your Janus installation, it mistakenly does. – Ingo Karkat Nov 05 '12 at 11:19

1 Answers1

3

I don't know Janus so this answer might not be 100% useful for you, but let's see. You could try finding out where the settings have been set. Try this:

Get current settings:

:set filetype? syntax?

Check where these have been set:

:verbose set filetype? syntax?

Execute these commands when you lost your syntax highlighting:

:syntax on
:set ft=python
:verbose set ft? syn?

Here you should see which script changed your filetype after saving. Normally, vim uses heuristics to determine the correct filetype if the file extension is ambiguous. In cases where these heuristics don't work, you usually set a global variable in your vimrc to a fixed value. In your case this would be something like:

let g:filetype_py="python"
steffen
  • 16,138
  • 4
  • 42
  • 81
  • When I run ":verbose set filetype? syntax?" I got filetype=conf Last set from /usr/share/vim/vim73/filetype.vim syntax=conf Last set from /usr/share/vim/vim73/syntax/syntax.vim – Jeremy Nov 06 '12 at 12:21
  • Now have a look at the filetype.vim. There should be a line ```au BufNewFile,BufRead *.py,*.pyw setf python``` - and later in that file you find a comment ```Generic configuration file``` which is probably responsible for your .conf setting after saving the file. Is the file extension still set after saving? See ```:echo expand('%:e')``` – steffen Nov 06 '12 at 13:07
  • I found that I can fix the problem by typing "autocmd BufNewFile,BufRead *.py,*.pyw set filetype=python" in my .vimrc.after – Jeremy Nov 06 '12 at 17:09
  • That's fine, keep this configuration. If you want to spend more time in this, you could adjust the filetype detection in your installation (see http://vimdoc.sourceforge.net/htmldoc/filetype.html). There's no need to, if it's very unlikely you're going to edit non-python files with .py extension. – steffen Nov 07 '12 at 08:50