Syntax highlighting randomly disappears when saving file

I use vim to edit some python files, and it has been sporadic lately that syntax highlighting disappears after saving the buffer inside vim. I tried to use reset syntax on and set filetype=python , but to no avail. I have no idea what causes this problem at all, so now I have minimal diagnostic information. But has anyone encountered this before, or where can things break?

+7
source share
3 answers

This is not a solution, but it is difficult to write / read in the comments.

I meant that I was messing around with syntax / highlighting when I started fixing my own .vimrc , which is why I noticed this. syntax on / syntax enable is only ready to download files (adding a large number of au to BufNewFile / BufRead ). Therefore, if any plugin fiddles with the syntax / highlight settings when writing a file, the file must be loaded again for all the "magic", this is not enough with the installation of filetype . Do :au BufRead , and you will see all the auto commands added at the start of the syntax. But the file must be downloaded to get all the settings.
See this:> syntax-loading

If you do not want to reload the file, try syntax enable , I think this is different from syntax on .
Or try also do :doautocmd filetypedetect BufRead % , see> autocmd-execute

I don’t know what causes the problem, right? Should you add some auto commands or create your own colors / syntax?
Otherwise, until you get a solution, you can try adding autocmd BufWritePost * <with the commands above that works> at the end of your .vimrc , use augroup in this case.

Here is an example:

 augroup myResetSyntax au! autocmd BufWritePost * syntax enable | doautocmd filetypedetect BufRead "%" augroup END 
+5
source

You can also restore the syntax by reloading the buffer, simply:

 :e 
+2
source

Now, after a while, I understand that the comic highlighting is caused by somersault, and not by any mysterious forces associated with saving files, so a simple fix * would be to do the following in my vimrc

 noremap <F9> <Esc>:syntax sync fromstart<CR> inoremap <F9> <Co>:syntax sync fromstart<CR> 

and press F9 whenever something happens.

  • Solution found from here
+1
source

All Articles