103

When I execute Rmodel, Rcontroller and others in Vim. I see only white text. But when I go to next buffer and then go back by :bn and :bl, colors are working.

This is my .vim folder https://github.com/regedarek/dotvim

7 Answers 7

121

Use :syntax sync fromstart

I got that tip from http://vim.wikia.com/wiki/Fix_syntax_highlighting

That article also suggests creating a mapping for that command e.g. to map F12:

noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>
Sign up to request clarification or add additional context in comments.

5 Comments

This works, but removes underlining for spell checking. Any way to prevent that?
Sorry I don't know. I don't have anything doing spellchecking. It may depend on your particular addon. If no one else answers, it may be worth composing a question w/ all the relevant information.
I end up using autocmd BufEnter,InsertLeave * :syntax sync fromstart to force a syntax sync after I enter a buffer. The syntax sync is also triggered when there's an InsertLeave event happening.
Maybe someone will find this useful one day. Use filetype=sh or syntax=sh for bash and like, not bash. Took me sometime to figure that one out.
I am using :do Syntax equivalent of :doautocmd Syntax which is just a little shorter so you don't have to bind it (at least I don't have to call this command that often so I just type it). I mostly use it in some files that contain regexes which cause the syntax highlight to break when I open the file via netrw.
74

Try:

:e

If you have no unsaved changes to the file.

Or:

:syn off | syn on

4 Comments

Create a mapping to ease the pain, something like nmap <F6> :Rmodel|e<CR>
It seems there should be some builtin command to refresh syntax highlighting. Something else that seems to work is to jump to another part of the file, like the beginning or the end, and jump back, e.g.: gg``
":syntax sync fromstart" (without the quotes) did not reload filetype syntax highlighting on files that sometimes do not show syntax coloring -- an ongoing issue for me and Vim, for some reason. However, ":syn off | syn on" immediately worked. ":e!" will also accomplish that reloading, but beware: unsaved changes to your file will be lost!
:syn off | syn on changes the colourscheme for me, which is annoying
16

Tried all the above - none worked for me.

The only thing that does work is: :filetype detect so I remapped a quick shortcut :noremap <Leader>,ftd :filetype detect

Comments

10

Sometimes syntax is OK, but all that is getting broken are foldings. In that case zx helps alot.

From VIM documentation:

zx

Update folds: Undo manually opened and closed folds: re-apply 'foldlevel', then do "zv": View cursor line. Also forces recomputing folds. This is useful when using 'foldexpr' and the buffer is changed in a way that results in folds not to be updated properly.

Comments

5

Another option is:

doautocmd Syntax

2 Comments

fwiw, this is the only answer that fixed syntax highlighting when I had this problem (syntax sync fromstart did nothing, nor did anything else!)
A shorter version of this would be :do Syntax
-1

You can also try:

CTRL-L

From the VIM docs (:h CTRL-L):

Clear and redraw the screen. The redraw may happen later, after processing typeahead.

1 Comment

Ctrl-L refreshes the screen. It makes no difference if you made changes to the corresponding syntax file.
-2

Try:

:Rrefresh!

This command refresh certain cached settings and also reload rails.vim.
It's help me refresh syntax highlighting in Rails.

1 Comment

This only applies to rails.vim and is not a general solution. Without that plugin, this command does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.