39

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
  • 6,966
  • 11
  • 52
  • 72

10 Answers10

47

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
  • 34,610
  • 8
  • 97
  • 89
  • modeline Last set from ~/.vimrc – juanpablo Aug 16 '11 at 05:06
  • for security, I change the modeline to add this to my ~/.vimrc au BufNewFile,BufRead .*aliases set filetype=sh – juanpablo Aug 16 '11 at 05:24
  • Not all of them have been long fixed. https://github.com/numirias/security/blob/master/doc/2019-06-04_ace-vim-neovim.md – duckbrain Jun 14 '19 at 21:44
  • 2
    @duckbrain It is fundamentally impossible to guarantee that all security issues have ever been fixed so long as it is ever possible to include embedded instructions to be executed by the code looking at the data in the very data itself. This is why the documentation for Keith Bostic's *nvi* has always said of `modelines` that the option is UNimplemented and that it never will be. This is just like all the security bugs stemming from file-embedded commands or macro instructions to run in PDF, Word, Excel, &c&c&c. – tchrist Jun 17 '19 at 19:16
24

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.

Bruno Bronosky
  • 1,885
  • 1
  • 20
  • 26
  • This is not necessarily true for all vim installs. The form with `set` works fine for me on vim 8.0 (although not on 7.4). Also worth noting that with the second form `// vim: ai tw=75`, there cannot be any other characters at the end of the line, or vim will complain and ignore the modeline. – ash Apr 12 '17 at 15:56
  • This should be definitely reported as a bug (or as outdated documentations). I reproduced on `VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48) Inklusive der Korrekturen: 1-1689 Extra-Patches: 8.0.0056 Verändert von pkg-vim-maintainers@lists.alioth.debian.org Übersetzt von pkg-vim-maintainers@lists.alioth.debian.org Riesige Version mit GTK2-GNOME GUI. ` (The last lines mean *modified by ...*, *compiled by ...*, *huge version ...*). – LittleByBlue Sep 10 '17 at 08:36
  • I tried to address it on the mailing list. They disagreed. – Bruno Bronosky Sep 11 '17 at 21:03
  • 1
    @BrunoBronosky do you have link to the mail thread? Would like to read their reasoning. – graywolf Sep 15 '17 at 12:37
  • Here is a link to the thread. https://groups.google.com/d/msg/vim_dev/aFh3X0etB7c/zl0Vi-MuFQAJ I didn't really take the time to understand their reasoning. – Bruno Bronosky Oct 30 '17 at 23:40
  • Not working for me with set, just without it. nocompatible doesn't matter. Ubuntu 22.04 Vim 8.2. modeline is set in my .vimrc. By default nomodline was set in /usr/share/vim/vim82/debian.vim – Arpad Horvath Jul 28 '23 at 09:37
17

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

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
Asad R.
  • 407
  • 5
  • 10
  • 5
    This has no effect if `modeline` is not also set (which it is by default in Vim unless you're root) – kynan Sep 05 '13 at 12:10
  • Gentoo has a system-wide `set nomodeline` in `/etc/vimrc` due to all the vulnerabilities with modelines handling in vim. – MattBianco May 23 '14 at 13:43
  • In my case, I also had to set `modelines` to a non-zero value. The help says the default is `5`, so that's what I set it to and that did the trick for me. The states that, ```If 'modeline' is on 'modelines' gives the number of lines that is checked for set commands. If 'modeline' is off or 'modelines' is zero no lines are checked.``` – Ben Jun 10 '16 at 15:59
11

in ~/.vimrc you need to set following:

set nocompatible 
filetype plugin on
set modeline 
vahagn34
  • 119
  • 1
  • 2
8

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

set modeline
jonsca
  • 4,077
  • 15
  • 35
  • 47
scifisamurai
  • 101
  • 1
  • 5
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
  • 2
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,178
  • 7
  • 7
  • Thanks, and it must be after `set nocompatible` if you use that. On Ubuntu 18.04 and vim version 8.0.1453 – 244an Mar 11 '20 at 11:43
  • Should that be `~/.vim/.vimrc`? Saving as `~/.vim/vimrc` didn't work for me. – mrhd Mar 12 '20 at 19:44
  • @user727089 `~/.vimrc` prioritize `~/vim/vimrc`. The latter won't be used if the former is found first. – Simba Mar 13 '20 at 01:18
  • No, my question was specifically about the dot (`.`) in the file name. `~/.vim/.vimrc` works, `~/.vim/vimrc` does not. – mrhd Mar 14 '20 at 06:21
  • 2
    @user727089 No `.` is needed, at lease in vim 8. Referred from the help within vim 8 `:help vimrc`. Places for your personal initializations: Unix `$HOME/.vimrc` or `$HOME/.vim/vimrc`. – Simba Mar 14 '20 at 06:40
  • @Simba, you're right! I checked again and it works as stated by you. Thank you. – mrhd Mar 14 '20 at 17:07
1

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
1

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
  • 11
  • 1
-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
  • 241
  • 2
  • 5