开发者

VIM loses syntax highlighting when using split command

开发者 https://www.devze.com 2023-04-08 21:34 出处:网络
So I have created my own syntax highlighting file and it works well if there is only one file opened. However, if I do :split otherFile, the other buffer that gets opened does not have syntax highligh

So I have created my own syntax highlighting file and it works well if there is only one file opened. However, if I do :split otherFile, the other buffer that gets opened does not have syntax highlighting. I have tried various things, like :syntax on etc. What can be the problem?

I am running Ubuntu 11.04, 64-bit version.

VIM version: VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 24 2011 07:07:34)

I have created a simple syntax highlighting file and put it in ~/.vim/plugin/syntax.vim

The last line of the syntax highlighting file is let b:current_syntax = "sth". I haven't done any kind of wiring, like specifying the file locatio开发者_Go百科n in .vimrc, the syntax works automatically (for one file opened).


I had this problem recently and much more generally. That is, the problem appeared for all filetypes. After debugging a bit, I discovered that the filetype was being properly recognized, but that for new splits, syntax was somehow becoming unset.

I'm still not 100% sure how this happened, but for future Google visitors, I'll write down what fixed the trouble for me: I moved set syntax = on much earlier in my .vimrc. After years of accretion, the line with set syntax = on had drifted down until it was below a number of other things. Moving it back up to (nearly) the top of the file fixed things for me. Here's what the start of my .vimrc looks like now:

" First two uncommented lines only useful if you use Tim Pope's vim-pathogen.  
" https://github.com/tpope/vim-pathogen
execute pathogen#infect()
execute pathogen#helptags()

" Most general settings first
set nocompatible            " Vim rather than Vi; implied by this file
syntax on                   " Syntax highlighting on
filetype plugin indent on   " Filetype detection

" ... etc.


Syntax files belong into ~/.vim/syntax/sth.vim, not ~/.vim/plugin/syntax.vim. The latter is sourced only once on startup, that's probably why it only works for the first loaded file.

For your syntax to become active, you need to :setf sth, or insert a corresponding modeline into your files, or write a filetype detection for your syntax to automate that.


Resurrecting this, as I just observed the same behavior. The solution that I found was to add the filetype on setting to my .vimrc file. fwiw, I added it immediately after syntax on. syntax on happened to be at the top of my .vimrc file already. So, here are the first few lines of my .vimrc:

1 " Activates Syntax Highlighting 2 syntax on 3 4 " Filetype on 5 filetype on

0

精彩评论

暂无评论...
验证码 换一张
取 消