Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep syntax highlighting for a buffer for the duration of a vim session

Tags:

vim

When I open a file

 :e x

and change its filetype

 :set ft=plsql

I can see the effect of setting the filetype because the keywords in the buffer are highlighted according to the filetype (syntax) that I just set.

Then, I open another file

 :e y

and switch back to the file where I set the filetype

 :e #

the keyword-highlightening is lost, although the filetype is still in effect.

 :set ft?

confirms the file type is plsql.

So, how can I make vim so that it keeps the highlighting when I switch back to the file?

like image 436
René Nyffenegger Avatar asked Oct 02 '22 02:10

René Nyffenegger


1 Answers

I assume you have set nohidden (or rather, don't have set hidden). This means, any buffer that is not displayed somewhere is unloaded. Thus, :e y will make Vim completely forget about x. When you do :e #, you don't return to x: you reload it. Since it's a completely new buffer, your filetype is, of course, not there.

If you did have set hidden, opening y would not unload x even if it is not being displayed anywhere, and :e # would simply display the buffer containing x (which already has the filetype set). This also has the benefit of making Vim much less annoying, since you can load other files without having to write the current changes to disk (and without the constant pestering from Vim if loading another file when you have unsaved changes is something you really wanted to do).

EDIT: The explanation seems to be bogus. The fix is still set hidden. Go figure.

like image 171
Amadan Avatar answered Oct 03 '22 15:10

Amadan