3

In vim you can include modelines in comments at the start/end of a file, like

// vim:sw=4 ts=4 et

I regularly work with PHP and try to stick to PSR-2 but Drupal (of course *rolls eyes*) has to be different.

I would like to be able to include something like

// vim:codestyle=psr-2

or

// vim:codestyle=drupal7

and have this trigger various setup options in my .vimrc that define that code style, for example ts=2 sw=2 et for drupal7 and ts=4 sw=4 et for psr-2.

Is this possible? (well I suppose I'm hoping it is, so what I really mean is How can I do this?)

marked as duplicate by Martin Tournoij, 200_success, John O'M., muru, kenorb Mar 19 '15 at 11:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Mar 18 '15 at 14:50

This question came from our site for professional and enthusiast programmers.

  • This question might be a really good candidate for asking on Vi and Vim. You might consider flagging your question for a moderator to migrate there if you want to try your luck getting an answer there instead. I don't think this is possible without a special plugin that might be a pretty involved setup process. – Caleb Mar 17 '15 at 11:18
  • Wow I never knew that existed! Mixed feelings about migrating from a site with 16k questions on vim and close to a million users to one with 0.5k and c900... – artfulrobot Mar 17 '15 at 11:25
  • It's a beta site and yes, traffic and user base are a consideration given that this is on topic both places. You might find, however, that people are more willing to go in depth to provide solutions on the dedicated site rather than just brushing this off with a "nope" here and having it quickly get lost in the question stack. – Caleb Mar 17 '15 at 11:30
  • Yeah, good points. If it's going to happen I'll go with it, but the fragmentation on SE is not always helpful IMO (I regularly use unix/SO/SU/security/DBA and there's a lot of Xover). But I have sympathies for the beta sites - especially as I've been trying to get a CiviCRM one going! Will flag. – artfulrobot Mar 17 '15 at 11:36
3

Per the answers to a question I recently asked on the Vi and Vim Stack Exchange, the vim modeline can only be used to trigger a predefined subset of parameters to the :set command already built into vim. It sounds like you could hack something up using .exrc, but a more promising direction that is probably better for your use case is setting up per-project rc files using a plugin such as local_vimrc.

  • 4
    After further thinking, I've suggested another approach. The files could contain such kind of modeline: :vim set ft=php.psr2. And the OP would have to define a ftplugin ftplugin/psr2.vim that'll contain calls to :setlocal. – Luc Hermitte Mar 17 '15 at 13:14
1

I tend to support file type specific configs using filetype plugins. Then using ft=myType in my modeline:

// vim:set sw=2 ts=2 et ft=php fdm=marker:

And it all woks right.

You can also use autocommands to find file type and executs scripts.

Finally, you can use a local ./.vimrc if :set exrc is on.

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