tags:

views:

175

answers:

4

For example if I open a brand new file in vim it has the following text already in it:

/*
  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.
*/

Would be annoying to have to type/copy & paste it in for every new file.

+13  A: 

How about creating a textfile ~/.vim/mit.txt that holds the MIT license? Then the following in .vimrc:

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

...so you only have to enter :mit for inserting the license.

Or, if you really want this behavior all the time:

autocmd BufNewFile *
\ 0r ~/.vim/mit.txt
augroup END
kotlinski
I recommend ":0r ~/.vim/mit.txt" to insert it at the top of the file.
Tomalak
Thanks, corrected.
kotlinski
+3  A: 

You'd probably find it annoying eventually when you try to create a file that you don't want to have this license.

A couple other options that still make it easier, but give you control over it:

  1. Store the license text somewhere, let's say in ~/mitlicense. Then when you start a new file, run the command ":r ~/mitlicense", which will insert the contents of the file into the new one. If you want to speed this up even further, map a command that does that, or even assign it a keyboard shortcut.
  2. Define an "abbreviation" for the license (instructions here). Then all you have to do is type a short identifier, and vim will replace that identifier with the license. Something you wouldn't type by accident, like "#mit" would probably do the job.
Chad Birch
+2  A: 

Or look at the several templating systems: http://www.google.hu/search?&q=vim+templating . And you can use an autocommand for automatically do the already recommended ':map' command for every new file. And you can narrow this to specific file types.

Zsolt Botykai
Even better there is a category of tips dedicated to this subject on wikia: http://vim.wikia.com/wiki/Category:Automated_Text_Insertion , where most template-expander plugins have been listed.
Luc Hermitte
+3  A: 

As http://www.gnu.org/copyleft/gpl.html says,

To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.

So it may be useful to be able to auto insert some text. The snipMate plugin can be used for that purpose. Although it is designed for code snippets, it can be used to auto insert some arbitrary text too.

After installing the snipMate, append the following code block into the file in ~.vim\snippets_.snippets file.

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.
    */

Now, when you type

mit

and press

<Tab>

it will insert the text above. It will also adjust the date, and it will automatically move the cursor to the name and e-mail part.

So, you can add your licenses(GNU, MIT, you name it) into that file and use whenever you need them.

Caglar Toklu