15

As per the accepted answer to this question, I'm attempting to use modelines in vim to force filetype detection in some files.

For example, at the top of a file named gitconfig (note there is no leading .), I have the following line:

# vim: set filetype=gitconfig : 

modeline is enabled on my system. However, when I open the file in vim, set filetype? returns conf, rather than the expected gitconfig.

Is it possible that other parts of my vim configuration (e.g. filetype.vim) are causing this strange behaviour?

Edited in response to comments:

set compatible? returns nocompatible

set modeline? returns modeline

verbose set filetype? returns:

filetype=conf
      Last set from /usr/share/vim/vim73/filetype.vim

I don't understand why the system wide filetype plugin would be overriding what I have set in the file itself.

One final note: this is the version of Vim 7.3 shipped with OSX.

The latest version of MacVim running on the same system using the same .vimrc behaves as expected, with set ft? returning filetype=gitconfig.

4
  • I usually saw those options set at the end (last line) of files - maybe that is the issue... Sep 1, 2011 at 7:36
  • Just to make sure: did you check that set modeline? does not print nomodeline? Sep 1, 2011 at 8:11
  • I find the most common cause for modeline not working is that compatible is set. That would be worth checking too.
    – Edd Steel
    Sep 1, 2011 at 15:56
  • See updated question for responses. Sep 4, 2011 at 0:22

2 Answers 2

24

So, after some digging, it transpires that the system vimrc shipped with OSX sets the modelines (note the trailing 's') variable to 0. This variable controls the number of lines in a file which are checked for set commands. Setting modelines to a non-zero value in my .vimrc solved the problem.

Full output, for the curious: the output of vim --version prompted me to check the system vimrc:

% vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Jun 24 2011 20:00:09)
Compiled by [email protected]
Normal version without GUI.  Features included (+) or not (-):
...
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe
Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses

Looking at the system vimrc:

% cat /usr/share/vim/vimrc 
" Configuration file for vim
set modelines=0         " CVE-2007-2438
...

Led me to the modelines variable. It appears that MacVim does not source this system file (perhaps looking for a system GVIMRC instead? :help startup isn't clear).

VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Jul 27 2011 19:46:24)
MacOS X (unix) version
Included patches: 1-260
Compiled by XXXXX
Huge version with MacVim GUI.  Features included (+) or not (-):
...
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -p
ipe  -DMACOS_X_UNIX -no-cpp-precomp  -g -O2 -D_FORTIFY_SOURCE=1
Linking: gcc   -L.         -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -L/usr/local/lib -o V
im -framework Cocoa -framework Carbon      -lncurses  -liconv -framework Cocoa    -fstack-prote
ctor -L/usr/local/lib  -L/System/Library/Perl/5.10/darwin-thread-multi-2level/CORE -lperl -lm -
lutil -lc -framework Python   -framework Ruby
3

For this to work on my macOS system I had to define a couple of settings in ~/.vimrc:

modeline
modelines=2

Where 'modeline' enables the use of in-file settings, and 'modelines=#' sets the number of lines that vim/vi will check for set commands.

Once those parameters were set I was able to use syntax like the line below in my files to override set parameters.

# vim: ts=4 sw=4 ai

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .