I recently asked a question in which I learned that it is not possible to pass anything except parameters to :set in vim modelines. I understand the security concerns, but I'm wondering if there isn't another way around this. A recent question on SO about setting large batches of parameters to comply with a coding standard got me to thinking—would it be possible to define a "group" of things in my rc file that can be turned on or off from the modeline?

Excuse the pseudo code, but is there a way to create a function that is triggered from a set parameter?

set_function! codestyle()
    if &1 == 'psr-2'
        set ts=4
        set sw=4
    if &1 == 'drupal7'
        set ts=2
        set sw=2
        iab D8 Drupal 8

This could then be triggered by a modeline such as

# vim: codestyle=psr-2

This would be in some ways be similar to file type matching, but allow manual overrides. It would also setting up the environment in other pre-defined ways, avoiding the security issues of having a file run other arbitrary commands in vim but allowing a more customized environment. Is there a way to do this?

  • This would require defining your own option for set ; AFAIK, this is not possible in Vim ... Mar 17, 2015 at 12:31
  • @Carpetsmoker Does that mean the answer to this question is that vimscript cannot wrapper or otherwise extend built-in functions?
    – Caleb
    Mar 17, 2015 at 12:36

1 Answer 1


You have a few choices.

  1. First one: You can write your own kind of modeline decoder, i.e. a plugin. This is a very similar answer to the one I gave to your other question.

    Indeed, we cannot add new vim options (that can be set with :set), but we can add new variables. And we cannot use the standard modelines to do more than setting vim options.

  2. Second possibility: let-modeline could be exploited through the callback feature to call one function or another. You could have your CodeStyle() function in your .vimrc or a (autoload?) plugin, then have your files contains:

    # VIM: let b:codestyle='psr-2' b:ModeLine_CallBack='CodeStyle'

    But if your need is this precise and if you really don't want plugins like local_vimrc, then write your own modeline-like decoder.

  3. The third, and IMO, better choice in that case would be to define a new filetype and set 'ft' to this filetype in modelines with: # vim: set ft=php.psr2. With php being the global usual filetype (I'm not sure about this one), and psr2 being the specialized filetype that'll correspond to your coding styles. You'll have to define two ftplugins: ftplugin/psr2.vim and and ftplugin/drupal7.vim where you'll put your calls to :setlocal (and not :set).

  • 6
    Upvoted for option 3. I think this is the most semantically appropriate and Vim-suitable approach.
    – tommcdo
    Mar 17, 2015 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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