如何拥有python文件的自动标头

Hau*_*uke 4 python vim

这里描述了python中正确的头格式.

使用VIM或shell脚本,我希望将通常的元数据(如__author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__)添加到文件头中.SVN关键字也不错.将其添加到新文件是最相关的.将其添加到现有文件是一个奖励.

Ruslan的Blog为Emacs提供了解决方案.但我无法找到Python的解决方案.

没有Emacs的python在哪里做过?VIM可以从一个文件复制文本到另一个像这样,但也许有一个更好的方式.

act*_*imp 6

我强烈推荐使用snipMate插件.您可以轻松地为每种文件类型添加新剪辑,只需键入关键字并点击标签即可触发它们,例如您可以点击

header<TAB>
Run Code Online (Sandbox Code Playgroud)

并且将添加所有字段,您可以轻松地标记需要按文件填写的任何字段.在内置的python片段(vimfiles/snippets/python.snippets)中甚至已经有一个名为docs的片段,看起来它填写了类似的文档字符串元数据,您可以根据自己的需要轻松修改它们,这里是一个例子.剪辑格式:

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

使用snip中的反引号支持动态条目(Filename和g:snips_author作为上面的tabbable条目1和2的默认值).可以添加日期:

`system("date +%Y-%m-%d")`
Run Code Online (Sandbox Code Playgroud)

(来自snipMate帮助文档).


fre*_*ass 5

这是我的 C++ 文件模板:

/*****************************************************************************
 * @file <file_name>
 *
 * @date <date>
 * @author John Doe
 * @email jdoe@yourcompany.com
 *
 * @brief
 *
 * @detail
 *
 *****************************************************************************/
Run Code Online (Sandbox Code Playgroud)

这是我里面的东西~/.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()
Run Code Online (Sandbox Code Playgroud)

基本上,我读取替换标签和实际信息的模板文件,并在新创建的文件的开头插入结果。s:insert_description()每当 vim 创建一个新文件时都会调用该函数。这是由autocmd最后一行设置的。

您可以基于此代码并为 python 创建等效项。