Syntax highlighting randomly disappears during file saving

╄→尐↘猪︶ㄣ 提交于 2019-12-10 03:45:19

问题


I'm using vim to edit some python files and recently it occurs sporadically that the syntax highlighting disappears after I save the buffer inside vim. I tried to reset syntax on and set filetype=python but to no avail. I have no clue of what causes this problem at all, so right now I have minimal diagnostic info. But has anyone encountered this before, or where could things break down?


回答1:


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

I meant that I was messing around with syntax/highlighting when I start to fix my own .vimrc, thats why I noticed it.
syntax on / syntax enable is only preparing for file load stuff (adding a lot of au to BufNewFile / BufRead). So if some plugin is messing with syntax/highlight settings when writing the file, the file must be loaded again for all "magic" to happen, it's not enough with setting filetype. Do :au BufRead and you will see all autocommands added when starting syntax. But the file must be loaded then to get all settigs.
See this:> syntax-loading

If you don't want to reload the file, try syntax enable, I think that is different to syntax on.
Or try also doing :doautocmd filetypedetect BufRead %, see > autocmd-execute

I don't know what is causing the problem, can it be you? If you added some autocommands, or doing own colors/syntax?
Otherwise, until you get a solution, you can perhaps try to add autocmd BufWritePost * <with the commands above that works> at the end of your .vimrc, use augroup in that case.

Here is an example:

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



回答2:


Now after a while I realize the screwy highlighting is caused by folding rather than than any mysterious forces associated with file saving, so a simple fix* would be to put the following in my vimrc

noremap <F9> <Esc>:syntax sync fromstart<CR>
inoremap <F9> <C-o>:syntax sync fromstart<CR>

and press F9 whenever something screwy happens.

  • Solution discovered from here



回答3:


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

:e


来源:https://stackoverflow.com/questions/14779299/syntax-highlighting-randomly-disappears-during-file-saving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!