Vim is a good clone of vi, but it does not support the enormously powerful (and hideously insecure) modelines of the original. It does support it's own crippled version of modelines useful only for changing settings. The crippling is intended to go a long way towards closing the security hole of modelines.
One vim feature however is that it supports autocommands to be run on certain events, such as reading in a file buffer. This can be massaged into running any vim command script. I seized upon this feature and used it to implement modelines.
Basically it consists of two parts. First an entry into a .vimrc file setting an autocommand to run the script on all buffer reads. Second the script itself.
Vi compatible modelines are capible of executing any vi command including shell escapes. Improper activation of modelines will kill you. Do not try this at home, these stunts are performed by trained professionals.
:changewhich may break some (very) obscure modelines.
:", which I find awkward, and because they are not valid ex commands. E.g: "
vim:ts=8:" is a valid vim modeline, but "
:ts=8" is not a valid command in vim. If you want to execute these, have vim process the modelines itself.
While I have subjected these to some grueling tests, more bugs may be hiding. Three of the tests I applied:
vi: set noml | w! | ! env VIMINIT="set sm" vim % :
vi: e! % | w! | q :
Note: this was written in response to a question on Usenet asking how to do this. Some of the useless bits in there are left over from the question. "| w!" from the first line and "e! % | w! |" from the second may be removed with no change in function.
An infinitely looping "Hello World" for true vi or vim with my
autocommand modeline script.
vi:$d a|$y b|@b:
vi: /virus!$/w >>~/.signature : Eli's vi modeline .signature virus!
If for some reason you still want to use these, here is how to do it.
Put these two lines in your ~/.vimrc file to activate this system for all files:
" Modeline auotcommand
autocmd bufread * :so! ~/.modeline.vimrc
Alternatively, you can "
set exrc" in your ~/.vimrc
and put those two lines only in .vimrc's in subdirectories where
you want modelines to be executed.
:$-4,$y z | 1,5y x
^Wn"xpG"zp:1y y |v/\(ex\|vi\):.*[^ ]\+.*:/d
By changing the
^V^M in the above to a
literal <ctrl-W> and <ctrl-V><ctrl-M>, you will have
a working version of the script. In vi or vim you must type a <ctrl-V>
before entering each of the control characters.
Second a uuencoded version of the script file:
begin 644 .modeline.vimrc M.B0M-"PD>2!Z('P@,2PU>2!X"A=N(GAP1R)Z<#HQ>2!Y('QV+UPH97A<?'9I M7"DZ+BI;7B!=7"LN*CHO9 HZ)7,O+BI<*&5X7'QV:5PI.EPH+BI<*3HN*B]< @,B\*.B1S+R0O%@TQ+PHZ9R\N+WD@60HZ<2$*1SI >0HI end