Just like the topic says; is it possible to add a certain text string in the beginning of a file if I create a certain file type?

For example, if I create a random Python file with vim random.py. Can I configure my vim to automatically add #!/usr/bin/python at line 1?

I have a standard vim installation without any plugins or anything, I didn't even have a .vimrc in my /home so I manually created it. As you might have imagined by now, I am very new to using vim so my apologies if this is a dumb question.

I found How do I add skeleton text to new vim files? but I am not sure if that is what I am looking for, or how to do it. If it turns out that this question is a duplicate of the question linked, I will try to look it up furthermore once I get home from work.

Thanks in advance!


Thanks for all the answers and I am sorry that this question indeed was a duplicate.

  • You question is indeed a close duplicate of the other one. Either you do it by hand as suggested by the other post and by vim documentation, or you use one of the numerous template expander plugin. And instead of :r, you have :put= and many more alternatives if you don't want to maintain template-files Jun 17, 2016 at 11:50
  • You might also want to look at the various snippet solutions such as UltiSnips. Jun 18, 2016 at 7:47

3 Answers 3


You can accomplish it by placing

autocmd BufNewFile *.py norm i#!/usr/bin/python

in your .vimrc file. This should insert the text at the beginning of the first file, whenever you open a non-existing file with the file extension py.

The part norm i#!/usr/bin/python is simply commands which are executed when the file is opened. As it is a new file, the cursor vil automatically be on the first line, which is empty. Then norm treats the rest of the commands as you where in normal mode, which means that i takes you to insert mode and, when in insert mode #!/usr/bin/python simply types these characters.

  • Since all of the answers seems to work and are very similar I marked your answer out of sympathy reputaion-wise :)
    Jun 17, 2016 at 12:32
  • @ Well, thanks. The points can sometimes be difficult to come by :)
    – Kristian
    Jun 17, 2016 at 13:31

You can use the following autocommand

autocmd BufNewFile *.py call append(0,'#!/bin/python')

Have a look at :h append()


Yup the question you linked should have helped you:

autocmd BufNewFile *.py normal! ggI#!/bin/python

In your vimrc will create an autocomd which will trigger when you create a new file BufNewFile of the filetype python *.py.

When It is trigger it will go to the first line of the file gg, start insert mode I and add the shebang line


As the autocommand triggers on BufNewFile the file is empty since it has just been created, the gg is not necessary. Also using i instead of I will not change anything as the line is empty.

  • you do not need gg as BufNewFile only applies to non-existing files (see :help BufNewFile). If you where to use BufNew,, then it would apply to all .py files.
    – Kristian
    Jun 17, 2016 at 11:32
  • @Kristian Yup that's what my edit said :-)
    – statox
    Jun 17, 2016 at 11:34
  • 1
    @statox: Ahh, sorry :) I misunderstood your edit then. I thought what you where saying was that, if the file is empty, it is not necessary, rather than it is always empty. By the way, my comment on BufNew was wrong. It does not just work on all .py files.
    – Kristian
    Jun 17, 2016 at 11:40

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