How to set any Vim file which i create to have appended with #! /usr/bin/env python by default

StackOverflow https://stackoverflow.com/questions/23646434

  •  22-07-2023
  •  | 
  •  

Question

I am need of a feature to ease my work:

The requirement is whenever i create a file with vim say vim welcome.py

The file should open with #! /usr/bin/env python appended to it by default.

How to do this. somebody please explain.

Thanks

Was it helpful?

Solution

add following line to ~/.vimrc

autocmd BufNewfile *.py silent! execute '0r! echo "\#\! /usr/bin/env python"'

EDITED: this one is more simpler

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

you can see the help by executing :help :autocmd in vim, or see http://learnvimscriptthehardway.stevelosh.com/chapters/12.html

Also append's help is in :help append().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top