5

A proper header format in python is described here .

Using either VIM or a shell script, I would like to have the usual metadata (like __author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__) added to the file header. SVN keywords would also be nice. Adding it to new files is the most relevant. Adding it to existing files is a bonus.

Ruslan's Blog has a solution for Emacs. But I was unable to find a solution for Python.

Where has this been done for python without Emacs? VIM can copy text from one file to another like so , but maybe there is a nicer way.

Community
  • 1
  • 1
Hauke
  • 2,554
  • 4
  • 26
  • 29
  • File creation and edit dates, and authorship, and version information are all maintained by source control. I don't understand the use case for adding a redundant (and possibly inaccurate or out of date) copy of this information in the file itself. Put your copyright/license info into your LICENSE file, and keep it out of your source code. It serves no purpose there. Project version info should exist once somewhere in the project, not in the header of every file. Contact information goes in your README. In short, none of these tags belong at the top of source code. – Jonathan Hartley Aug 30 '13 at 07:45

3 Answers3

6

I highly recommend the snipMate plugin. You can easily add new snips per file type, and they're triggered by just typing a keyword and hitting tab, so for example you could just hit

header<TAB>

and all the fields would be added, and you can easily tab through any that require being filled out on a per-file basis. There is even already a snippet called docs in the built-in python snippets (vimfiles/snippets/python.snippets) that looks like it fills out similar metadata for docstrings which you could easily modify for your purposes, here it is as an example of the snip format:

# Module Docstring
snippet docs
    '''
    File: ${1:`Filename('$1.py', 'foo.py')`}
    Author: ${2:`g:snips_author`}
    Description: ${3}
    '''

Dynamic entries are supported using backticks in your snip (Filename and g:snips_author as the default for tabbable entries 1 and 2 above). The date could be added with:

`system("date +%Y-%m-%d")`

(from the snipMate help doc).

actionshrimp
  • 5,219
  • 3
  • 23
  • 26
5

Here is my C++ files template:

/*****************************************************************************
 * @file <file_name>
 *
 * @date <date>
 * @author John Doe
 * @email jdoe@yourcompany.com
 *
 * @brief
 *
 * @detail
 *
 *****************************************************************************/

And here is what I have inside ~/.vimrc:

" Reads the template file replacing the tags by the actual
" information and insert the result at the beginning of the buffer. At
" the end, creates two blank lines at the end of the file and
" position the cursor at the first one.
function! s:insert_description()
    let template = $HOME . "/.vim/template/cpp.template"
    let file_name = expand("%:t") " Get file name without path
    let date = strftime("%Y") " Get the current year in format YYYY
    let i = 0
    for line in readfile(template)
        let line = substitute(line, "<file_name>", file_name, "ge")
        let line = substitute(line, "<date>", date, "ge")
        call append(i, line)
        let i += 1
    endfor
    execute "normal! Go\<Esc>k"
endfunction
autocmd BufNewFile *.{c++,cpp,cc,c,h,hpp} call <SID>insert_description()

Basically, I read the template file replacing the tags and with the actual information and insert the result at the beggining of the newly created file. The function s:insert_description() is called whenever vim created a new file. This is set by the autocmd at the last line.

You can base yourself at this code and create the equivalent for python.

freitass
  • 6,542
  • 5
  • 40
  • 44
0

Just go over to http://www.vim.org and search there for "new file template" and you'll get tons of plugins that solve this kind of problem, so look at them and pick one.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172