43

I have a file called .aliases for bash and zsh, I put this line

# vim: set filetype=bash: 

but if I make

: echo &ft

I get conf

juanpablo
  • 7,424

10 Answers10

53

That should work. I tried that line myself and it worked. I could be that your 'modeline' option got set to 'nomodeline'. Try executing this and see what you get.

:verbose set modeline?

That will tell you the value of the 'modeline' option and if not the default, where it was last set.

Some Linux distributions set 'nomodeline' in /etc/vimrc or /usr/share/vim/vimrc as a security measure, even though the security problems with 'modeline' have long been fixed.

garyjohn
  • 36,494
27

I describe a full debug checklist in this other answer.

I was REALLY stumped on this one because the documentation is not entirely true.

It turns out that in version 8 (and maybe earlier) you cannot use the word set in your modeline. The documentation describes "the second form" as being /* vim: set ai tw=75: */ but this does not work. You have to use "the first form" // vim: ai tw=75

Note: You can use either of those kinds of comment indicators. Or none at all.

18

set modelines=1 (or any non-0 value) in my ~/.vimrc got it working for me.

Asad R.
  • 417
12

in ~/.vimrc you need to set following:

set nocompatible 
filetype plugin on
set modeline 
vahagn34
  • 129
8

Adding just the following to my ~/.vimrc worked for me on my Funtoo Linux box:

set modeline
jonsca
  • 4,084
4

Since this seems to come up in searches:

I had the same problem:

# vim: set filetype=sh:

didn't work, resulting in ft=conf as well. without the modeline in my ~/.bash_alias, ft is empty, so something changes.

while

# vim: filetype=sh:

worked. the last ":" presence seems to be irrelevant.

I'm on OSX with a Vim8 brewed version, for the records.

it's weird because from the modeline help both

[text]{white}{vi:|vim:|ex:}[white]{options}

and

[text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text]

seem to be supported.

The output of

verbose set ft?

with "vim:filetype=sh" is

filetype=sh
      Last set from modeline

without modeline:

filetype=

with "vim:set filetype=sh"

filetype=conf
     Last set from /usr/local/Cellar/vim/8.0.1350/share/vim/vim80/filetype.vim

There might be a side effect somewhere in my conf, but the non compatible modeline syntax is the only one that works as expected for me.

kalfa
  • 41
3

/usr/share/vim/vim80/debian.vim from vim-common on Debian-based distros disables modeline by default.

" modelines have historically been a source of security/resource
" vulnerabilities -- disable by default, even when 'nocompatible' is set
set nomodeline

You have to enable modeline explicitly in your .vimrc or ~/.vim/vimrc file.

set modeline
Simba
  • 1,239
3

To make sure it works, set both options in your .vimrc file (and towards to bottom in case you have a more complicated .vimrc):

set modeline
set modelines=10

The 2nd option will control how many lines to check when searching for potential modeline lines. In case you need to use more than 10 such modeline lines, increase the value (10 in the example above)

And read also the NOTE in the documentation: :help modelines:

NOTE: 'modeline' is set to the Vi default value when 'compatible' is set and to the Vim default value when 'compatible' is reset.

Now depending how complicated .vimrc files one might have, changing the value of the compatible option (either directly or indirectly by certain plugins, could also influence the modeline behaviour.

But your file should listen now to such comment lines (here an example from my ~/.zshrc):

# vim: tabstop=2 shiftwidth=2 expandtab
2

Yup, even macOS, even as recent as Big Sur, still sets modelines=0 in the system-wide /usr/share/vim/vimrc. So you need to set modelines=1 in your ~/.vimrc to override this.

kode54
  • 21
-1

Most probably that's due to modeline being disabled.

I work around that by:

  1. keeping it disabled generally to avoid any security issues.
  2. Installing securemodelines plugin.
  3. Whitelisting only the modelines I use.

In this case, the default whitelisted commands include filetype.

weshouman
  • 263
  • 2
  • 5