Source From Here
Question
Today I was asked a question about defining custom extensions for vim syntax highlighting such that, for example, vim would know that example.lmx is actually of type xml and apply xml syntax highlighting to it. I know vim already automatically does it not just based on extension but by looking for certain strings inside the text, like but what if my file doesn't have such strings?
How-To
After digging around I found the solution. Take file extension .scala for example. Originally, vim can't recognize it and no syntax highlight:
Below action will let vim to handle file extension .scala with Java syntax high-light. Add the following to ~/.vimrc (the vim configuration file):
After applying it, try to edit Account.scala again:
But why and how does it work, you ask?
Today I was asked a question about defining custom extensions for vim syntax highlighting such that, for example, vim would know that example.lmx is actually of type xml and apply xml syntax highlighting to it. I know vim already automatically does it not just based on extension but by looking for certain strings inside the text, like but what if my file doesn't have such strings?
How-To
After digging around I found the solution. Take file extension .scala for example. Originally, vim can't recognize it and no syntax highlight:
Below action will let vim to handle file extension .scala with Java syntax high-light. Add the following to ~/.vimrc (the vim configuration file):
- syntax on
- filetype on
- au BufNewFile,BufRead *.scala set filetype=java
But why and how does it work, you ask?
- :help au :au[tocmd] [group] {event} {pat} [nested] {cmd}
- Add {cmd} to the list of commands that Vim will execute automatically on {event} for a file matching {pat}.
- :help BufNewFile When starting to edit a file that doesn't exist.
- :help BufRead When starting to edit a new buffer, after reading the file into the buffer.
- :help filetype will actually tell this whole story in part B.
沒有留言:
張貼留言