Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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: "au BufRead, BufNewFile *.jak set filetype=jak" to the top as described in the vim reference
  • I restarted vim (:x, and then reopened)

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

~/.vim/ftdetect][505]% cat jak.vim
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:

~/.vim/ftdetect][508]% cat ../../.vimrc
"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: http://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

Note: I completed all the quotes (comments) to make it easier to read


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.

share|improve this question
1  
in what sense do the *.c statements conflict with the *.jak statement? –  akira Mar 3 '10 at 5:59

4 Answers 4

up vote 7 down vote accepted
+100

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....

share|improve this answer
    
trying right now... But do you know what the .vim/ftdetect/ folder does? The behavior I am experiencing is that any formatting file in there applies to all file types. –  sixtyfootersdude Mar 2 '10 at 14:36
    
Huh, that partially worked.. However there was a conflict with two other lines in my vimrc. See my update. –  sixtyfootersdude Mar 2 '10 at 16:59

I tried reproducing what you've done, but couldn't get the filetype applied to other extensions.

I also found that this helped:

The Vim help-file for 'new-filetype' mentions creating your own 'filetype.vim' in ~/.vim/filetype.vim and writing your auto commands in that file:

if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  au! BufRead,BufNewFile *.jak      setfiletype jak
augroup END

All those syntax and highlight commands from your jak.vim need to stored under ~/.vim/syntax, so copy it to ~/.vim/syntax/jak.vim (without the au command).

You'll also have to quit Vim and restart to pick up the changes.

share|improve this answer

I must admit, I don't know for certain how useful this would be to you... But...

http://beerpla.net/2008/04/02/how-to-add-a-vim-file-extension-to-syntax-highlighting/

I have appended the text incase the page is taken down... Or altered...

How To Add A File Extension To vim Syntax Highlighting Posted by Artem Russakovskii on April 2nd, 2008 in Databases, Linux, Programming 24 delicious saves 2 diggs Share 3retweet

Updated: July 8th, 2009

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

After digging around I found the solution. Add the following to ~/.vimrc (the vim configuration file):

1 2 3 syntax on filetype on au BufNewFile,BufRead *.lmx set filetype=xml After applying it, my .lmx file is highlighted:

Same principle works, for instance, for mysql dumps that I have to do from time to time. If they don't have a .sql extension, you'll get something like:

After

1 2 3 syntax on filetype on au BufNewFile,BufRead *.dump set filetype=sql everything is fine:

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. And that's how you do it, folks.

share|improve this answer
    
Huh, going to have better look into this soon, but looks useful.. –  sixtyfootersdude Mar 1 '10 at 17:13
    
Well, that may potentially solve one problem but I don't think that it will solve this prob... I think that the key to this has to do with the .vim/ftdetect directory... –  sixtyfootersdude Mar 1 '10 at 17:19
    
I honestly don't even use VIM, so I'll have to take your word on it... –  Benjamin Schollnick Mar 1 '10 at 19:22

I think things might be twisted around, but I'm no vim expert.

Your syntax highlighting file should be in the syntax directory. The things in nsharish's advice might be what goes into the file you place inside of ftdetect.

This part:

au BufRead,BufNewFile *.jak set filetype=jak

In .vimrc, you may also need to add a line like:

au! syntax jak source $VIM/syntax/jak.vim

IMHO, it makes more sense this way. I hope this helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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