1

This is what I have in my /usr/share/vim/vim80/filetype.vim (the only mention of messages in the file) :

" Messages (logs mostly)
au BufNewFile,BufRead */logs/couchdb/{,.log}{,.[0-9]*,-[0-9]*} setf couch_log
au BufNewFile,BufRead */log/{auth,cron,daemon,debug,kern,lpr,mail,messages,news/news,syslog,user}{,.log,.err,.info,.warn,.crit,.notice}{,.[0-9]*,-[0-9]*} setf messages

The following file exists : /usr/share/vim/vim80/syntax/couch_log.vim

When I open ~/logs/couchdb/test.log, I am getting a filetype=messages instead of the expected filetype=couch_log.

What am I missing ?

11
  • 4
    What is the output of :verbose set ft? – Maxim Kim Nov 21 '19 at 10:15
  • Sorry was out of the office : filetype=messages Last set from ~/.vimrc So it looks like it's not looking in the file that I've set up... – Chapo Nov 22 '19 at 1:17
  • even after deleting the .vimrc file it still does not work. Now there is no filetype at all – Chapo Nov 22 '19 at 1:31
  • set nocp and filetype plugin on should have been in your vimrc – Maxim Kim Nov 22 '19 at 4:21
  • do not use :setf, but instead rather use :set filetype= – Christian Brabandt Nov 22 '19 at 7:31
2

You seem to have a bug in your filename glob pattern. The one you showed:

*/logs/couchdb/{,.log}{,.[0-9]*,-[0-9]*}

It will indeed match files inside a logs/couchdb/ directory, but the last part will only match files named .log or .789 or -789 or .log.789 or .log-789. Yes, these are full filenames, only filenames starting with . or - are valid. If you want to match any files with a log extension, you need to use *.log instead. You can then add the second part to also include numbered log files.

So, for example, it seems to me this would work:

*/logs/couchdb/*.log{,.[0-9]*,-[0-9]*}

This would match test.log or test.log.789 or test.log-789.

Or, you might want to just keep it simple and match on the directory name, match any filename, regardless of extension:

*/logs/couchdb/*

I can't really explain why you're getting a filetype of messages on that test.log, since that second pattern isn't really matching that filename either... So I can't really explain it. You might want to use :verbose set filetype? to understand where it's being set (it will list the rule where it's coming from, with the file and line, so you can easily find it.)

Assuming the pattern with the proper * wildcard for the filename solves your issue, there are a couple other things you might want to consider.

First, instead of modifying file /usr/share/vim/vim80/filetype.vim of your Vim installation, create a new user-local file for your user instead.

Create a directory ~/.vim/ftdetect. Inside it, create ~/.vim/ftdetect/couch_log.vim with the single line:

au BufNewFile,BufRead */logs/couchdb/*.log{,.[0-9]*,-[0-9]*} set filetype=couch_log

If you really want this configuration to be sustem-wide, you can do the same with a ftdetect directory under $VIMRUNTIME (probably somewhere under /usr/share/vim in your system, check :echo $VIMRUNTIME to find out exactly where.)

The main advantage of not modifying a system file is that you won't lose the setting if you upgrade or reinstall Vim on that machine, which would probably overwrite the file you modified. Also, by keeping it inside your .vim, it's easy to take that with you to a different machine, since you can transport all your configurations together from a single ~/.vim directory.

You don't need to create an augroup for this rule, Vim does that automatically for you as it sources *.vim files from the ftdetect directory.

Finally, you'll probably want to use set filetype=couch_log, rather than setf couch_log, in your filetype detection override.

The reason for it is that set filetype=couch_log will always set the filetype unconditionally, even if the file name has already matched another pattern and has been assigned a filetype in a previous rule. setf couch_log, on the other hand, will only set the filetype if one wasn't set yet. So you might end up skipping the setting if the file previously matched another filename pattern and was assigned a filetype already.

Since you're writing your overrides, you'll probably want to always use the stronger set filetype=..., since you probably want your rule to "win" in case a file matches multiple rules.

See :help ftdetect, it has more details on Vim's handling of the ftdetect directory and also spells out the differences between set filetype and setf.

2
  • 1
    thank you very much that was it – Chapo Dec 11 '19 at 5:52
  • 1
    Will also follow your advice re:creating my own directory etc. Thank you ! – Chapo Dec 11 '19 at 5:59
1

Try the following minimal vimrc:

set nocompatible
syntax on
filetype plugin indent on

For filetypes to be properly detected filetype should be on (:h filetype). By default it is off when vim is in nocompatible mode which might be the case here.

Not sure what was in your vimrc before you have deleted it, but it looks like filetype=messages was somehow set from it.

PS

I can't find that couch_log.vim in my distribution, so I suspect this is external thing and you want to detect a filetype and apply it.

Use the following in minimal vimrc:

set nocompatible
syntax on
filetype plugin indent on

au BufNewFile,BufRead */logs/couchdb/{,.log}{,.[0-9]*,-[0-9]*} set ft=couch_log

NOTE: there is difference in command compared to your initial one: set ft=couch_log instead of setf couch_log.

Although proper way to do it is to follow what vim help states in :h ftdetect

                            *ftdetect*
   1. Create your user runtime directory.  You would normally use the first
      item of the 'runtimepath' option.  Then create the directory "ftdetect"
      inside it.  Example for Unix: >
    :!mkdir ~/.vim
    :!mkdir ~/.vim/ftdetect
<
   2. Create a file that contains an autocommand to detect the file type.
      Example: >
    au BufRead,BufNewFile *.mine        set filetype=mine
<     Note that there is no "augroup" command, this has already been done
      when sourcing your file.  You could also use the pattern "*" and then
      check the contents of the file to recognize it.
      Write this file as "mine.vim" in the "ftdetect" directory in your user
      runtime directory.  For example, for Unix: >
    :w ~/.vim/ftdetect/mine.vim

<  3. To use the new filetype detection you must restart Vim.
15
  • Thks for taking the time. I created a .vimrc file containing your recommendations but no luck. It still does not pick up the correct file type. – Chapo Nov 22 '19 at 6:15
  • 1: /usr/share/vim/vimrc 2: /usr/share/vim/vim80/debian.vim 3: /usr/share/vim/vim80/syntax/syntax.vim 4: /usr/share/vim/vim80/syntax/synload.vim 5: /usr/share/vim/vim80/syntax/syncolor.vim 6: /usr/share/vim/vim80/filetype.vim 7: /usr/share/vim/vim80/ftplugin.vim 8: /usr/share/vim/vim80/indent.vim 9: ~/.vimrc 10: /usr/share/vim/vim80/syntax/nosyntax.vim 11: /usr/share/vim/vim80/plugin/getscriptPlugin.vim 12: /usr/share/vim/vim80/plugin/gzip.vim 13: /usr/share/vim/vim80/plugin/logiPat.vim – Chapo Nov 22 '19 at 6:15
  • 14: /usr/share/vim/vim80/plugin/manpager.vim 15: /usr/share/vim/vim80/plugin/matchparen.vim 16: /usr/share/vim/vim80/plugin/netrwPlugin.vim 17: /usr/share/vim/vim80/plugin/rrhelper.vim 18: /usr/share/vim/vim80/plugin/spellfile.vim 19: /usr/share/vim/vim80/plugin/tarPlugin.vim 20: /usr/share/vim/vim80/plugin/tohtml.vim 21: /usr/share/vim/vim80/plugin/vimballPlugin.vim 22: /usr/share/vim/vim80/plugin/zipPlugin.vim 23: /usr/share/vim/vim80/scripts.vim – Chapo Nov 22 '19 at 6:15
  • is the output of :scriptnames – Chapo Nov 22 '19 at 6:16
  • Before I deleted the .vimrc it was just a line to assign the type messages to .log files – Chapo Nov 22 '19 at 6:17

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.