2011-06-01 30 views
3

在python中正確的標題格式描述爲 here如何爲Python文件創建自動標題

使用VIM或shell腳本,我想將通常的元數據(如__author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__)添加到文件頭。 SVN關鍵字也會很好。將它添加到新文件是最相關的。將它添加到現有文件是一項獎勵。

Ruslan's Blog有一個針對Emacs的解決方案。但我無法找到Python的解決方案。

在沒有Emacs的情況下,python在哪裏? VIM可以將文本從一個文件複製到另一個文件 like so,但也許有更好的方法。

+0

文件創建和編輯日期以及作者身份和版本信息均由源代碼管理進行維護。我不明白用於在文件本身中添加冗餘(可能不準確或已過期)此信息副本的用例。將您的版權/許可證信息放入您的LICENSE文件中,並將其保留在源代碼之外。它在那裏沒有任何用處。項目版本信息應該在項目中的某處存在,而不是在每個文件的標題中。聯繫信息放在您的自述文件中。簡而言之,這些標籤都不屬於源代碼的頂部。 – 2013-08-30 07:45:16

回答

6

我強烈推薦snipMate插件。您可以輕鬆地添加新剪每個文件類型,他們正在通過直接鍵入關鍵字,打標籤引發的,因此,例如,你可以只打

header<TAB> 

和所有字段將被添加,您可以輕鬆地通過任何需要按文件填寫的選項卡。甚至在內置的python代碼片段(vimfiles/snippets/python.snippets)中已經有一個叫做docs的片段,它看上去就像填充了類似的docstrings的元數據,你可以很容易地修改它們來達到你的目的,這裏就是一個例子該剪斷格式:(:snips_author作爲默認tabbable條目1和2的上方文件名和g)

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

動態條目使用反引號在剪斷支持。日期可以加上:

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

(來自snipMate幫助文檔)。

+0

snipMate的有趣用途。我真的需要學習snipMate。 – john2x 2011-06-01 13:22:40

3

這裏是我的C++文件模板:

/***************************************************************************** 
* @file <file_name> 
* 
* @date <date> 
* @author John Doe 
* @email [email protected] 
* 
* @brief 
* 
* @detail 
* 
*****************************************************************************/ 

這裏是我裏面~/.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() 

基本上,我讀的模板文件替換標籤與實際信息和將結果插入新創建的文件的開始處。當vim創建一個新文件時,函數s:insert_description()被調用。這由最後一行的autocmd設置。

你可以根據這段代碼創建python的等價物。

0

只需轉到http://www.vim.org並在那裏搜索「新文件模板」,您將獲得大量解決此類問題的插件,請查看它們並選擇一個。