Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically inserting a header in vim

Is there a way to auto add a header when i open a new file in vim? My objective is to automatically add the shebang "#! /usr/bin/python" when i open a new file using the command "vim test.py". If the file is already present, no header should be inserted.

like image 270
Chander Shivdasani Avatar asked Nov 07 '12 22:11

Chander Shivdasani


People also ask

How do you go to top in vim?

Type "gg" in command mode. This brings the cursor to the first line.

How do I open a header file in vim?

Just add the "--extra=+f" option in the ctags line. You may then open new files manually with autocompletion, with :tag myfile. cpp Or you may adapt a script like a. vim or FSwitch.


2 Answers

Add this line in your configuration file:

autocmd BufNewFile *.py 0put =\"#!/usr/bin/python\<nl>\"|$
like image 191
Nicolas Avatar answered Oct 25 '22 12:10

Nicolas


This might be over-kill, but you could look at one of the snippet scripts for Vim, e.g. snipMate -- http://www.vim.org/scripts/script.php?script_id=2540

But, for what you want, you might just map a key to a command that reads in a file. For example:

nmap <leader>r :r boiler_mashbang<cr>

And, then put your boilerplate in the file: boiler_mashbang.

like image 37
Dave Kuhlman Avatar answered Oct 25 '22 11:10

Dave Kuhlman