Vim highlighting for specific file types (where to put syntax files, vim events, line to put into vimrc)

syntax highlightingunixvim

I have defined a file time jak.vim to offer custom highlighting when I take notes, however it is being applied to some files that do not have the .jak extension. Specifically a file named progress.jlog. Just to test if the problem was specific to that extension I renamed progress.jlog to progress (no extension) but experienced the same problem.

What I did:

  • I created jak.vim in the directory ~/.vim/ftdetect
  • I added this line to the top, as described in the vim reference
    • au BufRead, BufNewFile *.jak set filetype=jak
  • I restarted vim (:x, and then reopened)

This is what my ~/.vim/ftdetect/jak.vim looks like:

au BufRead, BufNewFile *.jak set filetype=jak

syn region JakeSubtitle start=+==+ end=+==+
highlight JakeSubtitle ctermbg=black ctermfg=DarkMagenta

syn region JakeTitle start=+===+ end=+===+
highlight JakeTitle ctermbg=black ctermfg=yellow

syn region JakeMasterTitle start=+====+ end=+====+
highlight JakeMasterTitle cterm=bold term=bold ctermbg=black ctermfg=LightBlue

syn region emphasis start=+<em>+ end=+</em>+
highlight emphasis ctermbg=black ctermfg=yellow

" makes all of the numbered items bold."
" (this works I just don't like the effect.  Decided to change to just highlight the "number)
"syn region numberedItem start=+^\t*\d*)+ end=+\n+"
syn match numberedItem +^\t*\d*)+
highlight numberedItem cterm=bold

And just incase you need to know this is what my .vimrc looks like:

"on will override defaults set.  Enable will allow you to set defaults."
" also turns on filetype"
"syntax on"
syntax enable

set nocompatible

" ???"
set backspace=2

"Auto indent"
set ai

"Map jj to Esc so that you do not have to reach for the Esc button"
imap jj <Esc>

"do not allow the search to wrap around the screen, must stop at the bottom."
set nowrapscan

"when doing a search highlight all occurances"
":set hlsearch"

"stop text from wrapping on the screen"
set nowrap

"turn the mouse on while in insert mode"
set mouse=i

"attempting to highlight specific keywords so it is easy to see in code."
"see help e410 for more info."
"see this post I created: https://superuser.com/questions/110054/custom-vim-highlighting"
"Legal colors: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta,"
"Brown, DarkYellow, LightGray, LightGrey, Gray, Grey, DarkGray, DarkGrey,"
"Blue, LightBlue, Green, LightGreen, Cyan, LightCyan, Red, LightRed, Magenta,"
"LightMagenta, Yellow, LightYellow, White"
syn keyword JakeKeywords        Question TODO Answer JAKEHTTPS PossibleProblem
highlight JakeKeywords cterm=bold term=bold ctermbg=black ctermfg=Blue


"for case-insensitve searches"
set ignorecase

"Override the 'ignorecase' option if the search pattern contains upper"
"case characters.  Only used when the search pattern is typed and"
"'ignorecase' option is on."
set smartcase


"use indents as the folding method"
set foldmethod=indent

"make vim save and load the folding of the document each time it loads"
"also places the cursor in the last place that it was left."
au BufWinLeave * mkview
au BufWinEnter * silent loadview

Update

I found nsharish's post to be very helpfull. They suggested that I add this to my vimrc:

au BufRead,BufNewFile *.jak set filetype=jak

and add my jak.vim file to ~/.vim/syntax

Unfortunately that code conflicts with these two lines (in my vimrc)

au BufWinLeave *.c mkview
au BufWinEnter *.c silent loadview

I use these two to save my folds, cursor location, etc when loading vim (see :help lo). If I comment out those two lines nsharish's suggestion works like a charm. With those two lines there is no highlighting in any of my files.

Conclusion

I marked nsharish's answer as the best answer (because it as most helpful to me). However this is how I solved the problem:

Nsharish was right I needed this line in my .vimrc:

syntax enable
au BufRead,BufNewFile *.jak set filetype=jak

And I needed to move my jak.vim file to ~/.vim/syntax.

However as noted above there was a conflict with these lines:

au BufWinLeave * mkview
au BufWinEnter * silent loadview

When these lines were commented the highlighting worked.

What I needed to do was to change the ...set filetype... to this:

au BufWinEnter,BufRead,BufNewFile *.jak set filetype=jak

I think that the BufWinEnter is called after the BufRead/BufNew file so the highlighting was being overwritten by the formatting saved from last time.

Thank again to nsharish for helping me to come up with this solution.

Best Answer

Did you try this..

  • Put your jak.vim in .vim/syntax folder
  • put the following lines only in your .vimrc file.
syntax enable
au BufRead,BufNewFile *.jak set filetype=jak
I tried this with your jak.vim file.... It worked fine for me....
I am using vim7.2...
edit:
Try this,
I had the same problem with those mkview and loadview lines... just set filetype once in the file and it will be retained then

Open the file, then do ":set ft=jak", save the file and quit vim.... Now reopen the file... syntax highlighting should work now...
mkview and loadview seems to save the last syntax highlight settings also....

Related Question