a cosmological pedestrian

vim modelines in git config

I’m working on different Open Source projects and most of them have different coding style guidelines. Mostly spaces or tabs or different tabwidth. The easiest thing would be to store these information in the git config of the project. So here is a easy and secure way to have modelines in the git config.

So first I set the modelines (here for the Samba project):git config --add vim.modeline "tabstop=8 shiftwidth=8 noexpandtab cindent"

The modeline you defined in your git config will be appended to the :setlocal command of vim. It only allows a limited set of setlocal commands to be used. It will not execute any arbitrary code and you probably are the only person modifying the git config. You can change the allowed commands by adding the following to your ~/.vimrc file

@Leif Looks like you introduce a security hole into your system. Someone could trick you to checkout a git repository which already has a .vimrc file in it. If you then open a file in that git project you would execute whatever arbitrary code the .vimrc file includes.

My approach is more secure here, cause a) you’re the only one who controls the git config file and b) it is limited to the :set command of vim 🙂

It may be better to hook it as autocommand when file is loaded. You should use :setlocal instead of :set in such case. The plugin work only when opening vim inside git tree and for example I am sometimes wandering in single session among two-three related projects.
Also if you have any plugin setting options on a per filetype basis, it will override those set in vim.modeline.

It would be nice to define modeline on per filetype basis: many projects are a mix of different languages and e.g. python “standard” (PEP8) says 4 spaces (sw=4 et) while some other files use different settings (for example Makefile: sw=8 noet)