1

At work I usually keep only a few files in buffer, typically a Yang file, a Java file, and possibly something like a readme file. In such an environment, the Yang syntax highlighting does not work. Example "run":

$ vim yangfile.yang # Yang syntax highlighting works
:e Foo.java         # new or existing file, doesnt'matter
:bnext              # Yang syntax highlighting is gone, all text is white
:so ~/.vimrc        # this does not work

My .vimrc is quite small, it starts with source ~/yang.vim (this file is available here) and then less than 50 more lines of pretty basic Vim configurations (I can edit my question to include the entire .vimrc on request).

How can I fix this problem? Thanks.

  • 1
    I think your setup is wrong—if that’s a syntax file, it should go into the syntax directory in one of the locations on your runtimepath. Then having syntax on in your vimrc will automatically load syntax highlighting (you may also need filetype plugin on; i cant remember). – D. Ben Knoble Jan 21 '18 at 9:50
  • Thanks. Where is the runtimepath? :set runtimepath? spit out several paths, ~/.vim was one of them. I moved my syntax file there, added syntax on to .vimrc, highlighting did not work, added filetype plugin on, didn't work either. More suggestions? :) – Erik Vesterlund Jan 21 '18 at 16:50
2

Thanks to David Ben Knoble's comment I was able to find relevant information and solve my problem. I did as follows and it worked for me:

In .vimrc, remove source ~/yang.vim, add :syntax on and :filetype plugin on (I added them to the top of the file). Inside vim (normal mode), type :set runtimepath?. In my case one of the directories printed was ~/.vim. Move yang.vim to ~/.vim/syntax (create if needed). Finally, create ~/.vim/ftdetect/yang.vim (create subdir if needed) and enter the following in that file:

au BufRead,BufNewFile *.yang set filetype=yang

Done and done. Useful links:

Vim 101: runtimepath

Creating your own syntax files

  • 1
    Well done! Glad you could solve the problem. Dont forget to mark it as answered – D. Ben Knoble Jan 21 '18 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.