Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To be specified,what I want to do is:

  • if I create xxx.php,#!/usr/bin/env php will be the first line of the file
  • if I create,#!/usr/bin/env python will be added first.

How can I approach this?Thanks!

share|improve this question
up vote 9 down vote accepted

After some searching work,I find a post in SO that can solve my problem using templates.Hope it's helpful for you guys.

Creat templates pythontmp.txt and phptmp.txt in some diretory(example:~/.vim/templates/)like below

#!/usr/bin/env python(php)

Add following setting to .vimrc

autocmd bufnewfile *.py :0r ~/.vim/templates/pythontmp.txt
autocmd bufnewfile *.php :0r ~/.vim/templates/phptmp.txt
share|improve this answer
+1 self answers are good for SU (this is also the right way to do it). – msw Jun 25 '10 at 8:00

Not sure you can set up vim to do this unassisted. You might create a helper script to do it for you though.

Assuming a linux environment... This script will check the file extension, and if necessary create the file with the header before calling vim.


type=`expr "\$1" : ".*\.\(.*\)"`   # Check the file extension

case "$type" in

if test ${interpreter+defined}; then echo "#!/usr/bin/env $interpreter" > $1 && /usr/bin/vim $1;
else /usr/bin/vim $1;

You could then alias this script to run in place of vim.

alias vim='/path/to/'
share|improve this answer
You could also investigate vim plugins/scripts – Tim Kane Jun 25 '10 at 7:16
Yeah i'm on that now .what's ${interpreter+defined} mean?I have to delete +defined and make it work. – SpawnST Jun 25 '10 at 7:19
It's a mechanism (using parameter expansion) to test if the $interpreter variable has been defined. You could just as easily write ${interpreter+blah} to have the same effect. ${var+word} # if var is defined, use "word"; otherwise, nothing – Tim Kane Jun 25 '10 at 7:41
oh i got you,thanks:) – SpawnST Jun 25 '10 at 7:52

There exist many plugins that offer this feature. Some even let you automatically deduce various things, or even parametrize your header depending the folder where you are creating your new file (mu-template).

share|improve this answer
+1 yes, lots of plugins which make the builtin around "autocmd" easier. it all runs under the terms of "templates" and "skeletons". – akira Jun 25 '10 at 9:37

I actually like the approach of defining a vim function to do the work, this way it becomes much easier to do more complicated things. For instance, if the inserted text depends on the name of the file as is commonly done with C/C++. My C/C++ one looks like this:

autocmd BufNewFile *.{h,hpp} call <SID>insert_c_gates() 


function! s:insert_c_gates()
   let gatename = <SID>get_gate_name()
   execute "normal i#ifndef " . gatename
   execute "normal o#define " . gatename
   execute "normal Go#endif /* " . gatename . " */"
   normal kk

The get_get_name() function forms the gate name from the name of the file being edited, and actually varies depending on the project I'm working on because some of the projects want it to be formatted a certain way.

share|improve this answer
I sense your solution is more powerful though not quite understand the function. – SpawnST Jun 25 '10 at 14:45
Actually, playing with :exe+:normal (/:put) is quite cumbersome to maintain comparing to solutions like mu-template (see…). Extracting the header generating function is quite easy as long as the Template Expander Plugin support calls to "external" viml functions. – Luc Hermitte Jun 25 '10 at 15:15
Yeah the point isn't the execute and normal combinations (I wrote those many years ago, and have not changed them since) ... but just the fact that by putting a function call in there you have access to all of vim's scripting power rather then just inserting a template. Plugins definitely provide some nice ease of use, but if you want to have your own solution without a plugin, the function approach is a good starting point for all solutions. – Neg_EV Jun 28 '10 at 18:53
Whats happening in my func is that I have a func get_gate_name that depending on the path of the file I determine its name(I keep my projects organized under a projects folder in a way the allows me to do this). Then once I have the name I insert the gates(in a very primitive way) by using the execute normal commands. Another reason I do this is that I wrote an UpdateGates func that given a file with existing gates I can update them to what I would like(for instance, if I rename a file). This allows me to reuse the functions where a template/plugin solution might not be as easy to do the same. – Neg_EV Jun 28 '10 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .