2

I have to edit a bunch of files in a programming language that has C-like syntax (and which may or may not be used only within the confines of my university).

To get syntax highlighting without going through the hassle of creating a new syntax configuration in Vim, I just use :set syntax=c to force the C syntax highlighting.

To get this automatically, I set syntax=c in my .vimrc file. The problem is, whenever I open a new file in a new window with :sp or :vsp, there is no syntax highlighting, which indicates that the set command in my .vimrc is only executed when I first open Vim itself.

How do I make it so this gets executed for every new file opened?

Alexandre
  • 386
  • 7
  • 15

2 Answers2

4

vim load syntax highlight file according to filetype. usually it is judged by file name extension. ex. if your programming language file with extension as .mine, you should add following configuration to .vimrc

au BufRead,BufNewFile *.mine   set filetype=c

You can refer to filetype.txt for more tricks.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
gzh
  • 3,507
  • 2
  • 19
  • 23
2

What you are looking for belongs in your ~/.vimrc. You don't need to set syntax to a specific file type, all you need is:

syntax on

That will invoke syntax highlighting for each file you open -- with the proper syntax file (vim is pretty smart...)

You can see the effect before you modify your ~/.vimrc simply by issuing the following within vim in command-mode

:syntax on
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I already have `syntax on` set in my `vimrc`, but of course I don't have a syntax file for this particular programming language -- that's something I was trying to avoid messing with. Since the syntax is quite C-like it suffices to just use the C syntax, and thus my usage of `set syntax=c`, which does work. My only problem -- and it's a really minor one -- is with opening with this syntax highlighting automatically at each new file I open in a same given Vim session. – Alexandre Sep 28 '17 at 03:53
  • I apologize, it appeared you were looking for `syntax on`. Does the file have a distinct file extension or file type? – David C. Rankin Sep 28 '17 at 03:57
  • It's alright. It's a text file with an `.mn` extension. I suspect the programming language itself was created by my professor "in-house" for usage in a Norma register machine simulator. Anyway, my problem has been solved by @gzh's answer. – Alexandre Sep 28 '17 at 18:00