2009-05-14 10 views
5

Per esempio se aprire un nuovo file di zecca in vim ha il seguente testo già in esso:C'è un modo per inserire il testo di copyright sui nuovi file in Vim?

/* 
    The MIT License 

    Copyright (c) 2009 Apphacker [email protected] 

    Permission is hereby granted, free of charge, to any person obtaining a copy 
    of this software and associated documentation files (the "Software"), to deal 
    in the Software without restriction, including without limitation the rights 
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
    copies of the Software, and to permit persons to whom the Software is 
    furnished to do so, subject to the following conditions: 

    The above copyright notice and this permission notice shall be included in 
    all copies or substantial portions of the Software. 

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
    THE SOFTWARE. 
*/ 

sarebbe fastidioso dover digitare/copiare & incollarlo nella per ogni nuovo file.

+4

Su un punto puramente estetico ... perché senti la necessità di metterlo in ogni file? Non puoi inserirlo in un file separato (ad esempio LICENSE.txt) e includerlo con tutto ciò che stai distribuendo? Le licenze nella parte superiore di ogni file nella distribuzione di un codice sorgente sono intrinsecamente fastidiose per chi legge/esamina i file di codice. –

risposta

19

Come creare un file di testo ~/.vim/mit.txt che contiene la licenza MIT? Poi il seguente in .vimrc:

map :mit :0r ~/.vim/mit.txt 

... in modo da avere solo inserire: mit per l'inserimento della licenza.

Oppure, se si vuole veramente questo comportamento per tutto il tempo:

autocmd BufNewFile * 
\ 0r ~/.vim/mit.txt 
augroup END 
+4

Raccomando ": 0r ~/.vim/mit.txt" per inserirlo nella parte superiore del file. – Tomalak

+0

Grazie, corretto. –

3

si sarebbe probabilmente trovare fastidioso alla fine quando si tenta di creare un file che non si desidera avere questa licenza.

Un paio di altre opzioni che ancora rendono più facile, ma consentono di controllare che:

  1. memorizzare il testo della licenza da qualche parte, diciamo in ~/mitlicense. Quindi quando si avvia un nuovo file, eseguire il comando ":r ~/mitlicense", che inserirà il contenuto del file nel nuovo. Se vuoi accelerare ulteriormente, mappare un comando che lo faccia, o anche assegnargli una scorciatoia da tastiera.
  2. Definire una "abbreviazione" per la licenza (instructions here). Quindi tutto ciò che devi fare è digitare un identificativo breve, e vim sostituirà quell'identificatore con la licenza. Qualcosa che non digiteresti per caso, come "#mit" probabilmente farebbe il lavoro.
2

Oppure guardare i diversi sistemi di modelli: http://www.google.hu/search?&q=vim+templating. Ed è possibile utilizzare un autocomando per eseguire automaticamente il comando ": map" già consigliato per ogni nuovo file. E puoi restringere a tipi di file specifici.

+1

Ancora meglio c'è una categoria di suggerimenti dedicati a questo argomento su wikia: http://vim.wikia.com/wiki/Category:Automated_Text_Insertion, dove sono elencati molti plugin di espansione template. –

5

Come http://www.gnu.org/copyleft/gpl.html dice,

Per fare questo, si inserisca i seguenti avvisi al programma. È più sicuro allegare all'inizio di ciascun file sorgente per indicare in modo più efficace l'esclusione della garanzia ; e ogni file dovrebbe avere almeno la riga "copyright" e un puntatore a dove viene trovata la notifica completa .

Quindi potrebbe essere utile poter inserire automaticamente del testo. Il plugin snipMate può essere utilizzato a tale scopo. Sebbene sia progettato per frammenti di codice, può essere utilizzato anche per inserire automaticamente del testo arbitrario.

Dopo aver installato lo snipMate, aggiungere il seguente blocco di codice nel file nel file ~ .vim \ snippets_.snippets.

snippet mit 
    /* 
     The MIT License 

     Copyright (c) `strftime("%Y")` ${1:Your name here} ${2:Your email here} 

     Permission is hereby granted, free of charge, to any person obtaining a copy 
     of this software and associated documentation files (the "Software"), to deal 
     in the Software without restriction, including without limitation the rights 
     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
     copies of the Software, and to permit persons to whom the Software is 
     furnished to do so, subject to the following conditions: 

     The above copyright notice and this permission notice shall be included in 
     all copies or substantial portions of the Software. 

     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
     THE SOFTWARE. 
    */ 

Ora, quando si digita

mit 

e preme

<Tab> 

che verrà inserire il testo di cui sopra. Regolerà anche la data e sposterà automaticamente il cursore sul nome e sulla parte e-mail.

Quindi, è possibile aggiungere le licenze (GNU, MIT, il nome) in quel file e utilizzarle quando ne avete bisogno.

2

Ho scritto un plug-in che fornisce i comandi per inserire le licenze nella parte superiore del buffer.

Here it is.

È possibile utilizzare un autocmd per automatizzare questo comportamento:

autocmd BufNewFile * Gpl 

Il vantaggio rispetto alle alternative delle altre risposte è che questo supporto plug-in più linguaggi di programmazione (che commenteranno il testo della licenza utilizzando l'opzione comments).

Inoltre, inserisce automaticamente l'anno corrente.

Disclaimer: Sono l'autore di questo progetto.

Problemi correlati