例如,如果我在vim中打开一个全新的文件,则其中已包含以下文本:
/*
The MIT License
Copyright (c) 2009 Apphacker apphacker@gmail.com
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.
*/
不得不输入/复制&将其粘贴到每个新文件中。
答案 0 :(得分:21)
如何创建保存MIT许可证的文本文件〜/ .vim / mit.txt?然后是.vimrc中的以下内容:
map :mit :0r ~/.vim/mit.txt
...所以你只需要输入:mit来插入许可证。
或者,如果你真的想要这种行为:
autocmd BufNewFile *
\ 0r ~/.vim/mit.txt
augroup END
答案 1 :(得分:5)
正如http://www.gnu.org/copyleft/gpl.html所说,
为此,请附上以下声明 到该计划。附着是最安全的 它们到每个源文件的开头 最有效地陈述 不包括保修;和每个文件 应该至少有“版权” 行和指向完整的指针 发现通知。
因此,能够自动插入一些文本可能很有用。 snipMate插件可用于此目的。虽然它是专为代码片段设计的,但它也可用于自动插入一些任意文本。
安装snipMate后,将以下代码块附加到〜.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.
*/
现在,当您输入
时mit
然后按
<Tab>
它会插入上面的文字。它还会调整日期,它会自动将光标移动到名称和电子邮件部分。
因此,您可以将许可证(GNU,MIT,您的名字)添加到该文件中,并在需要时随时使用。
答案 2 :(得分:3)
当您尝试创建一个不想拥有此许可证的文件时,您可能会发现它很烦人。
其他一些选项仍然可以让您更轻松,但让您控制它:
~/mitlicense
。然后,当您启动一个新文件时,运行命令“:r ~/mitlicense
”,这将把文件的内容插入到新文件中。如果您想进一步提高速度,请映射执行此操作的命令,或者甚至为其指定键盘快捷键。答案 3 :(得分:2)
或者看看几个模板系统:http://www.google.hu/search?&q=vim+templating。 您可以使用自动命令为每个新文件自动执行已建议的':map'命令。您可以将其缩小到特定的文件类型。
答案 4 :(得分:2)
我写了一个插件,提供了在缓冲区顶部插入许可证的命令。
您可以使用autocmd自动执行此行为:
autocmd BufNewFile * Gpl
其他答案的替代方案的优势在于此插件支持多种编程语言(它将使用comments
选项对许可文本进行注释)。
它还会自动插入当前年份。
免责声明:我是这个项目的作者。