代码之家  ›  专栏  ›  技术社区  ›  Bjorn

有没有一种方法可以将版权文本预先添加到Vim中的新文件中?

vim
  •  6
  • Bjorn  · 技术社区  · 16 年前

    例如,如果我在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.
    */
    

    如果要打字/复印,会很烦人;将它粘贴到每个新文件中。

    5 回复  |  直到 16 年前
        1
  •  22
  •   Johan Kotlinski    16 年前

    创建一个包含mit许可证的文本文件~/.vim/mit.txt怎么样?然后在.vimrc中执行以下操作:

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

    …因此您只需输入:mit即可插入许可证。

    或者,如果您真的一直想要这种行为:

    autocmd BufNewFile *
    \ 0r ~/.vim/mit.txt
    augroup END
    
        2
  •  6
  •   Caglar Toklu    16 年前

    http://www.gnu.org/copyleft/gpl.html 说,

    到节目中去。连接是最安全的 将它们添加到每个源文件的开头 最有效地陈述 排除担保;和每个文件 至少应该有版权 通知已找到。

    因此,能够自动插入一些文本可能很有用。这个 snipMate 插件可以用于此目的。虽然它是为代码片段设计的,但也可以用来自动插入一些任意文本。

    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,您可以随意命名)添加到该文件中,并在需要时使用它们。

        3
  •  3
  •   Chad Birch    16 年前

    当您试图创建一个您不想拥有此许可证的文件时,您可能最终会发现这很烦人。

    还有一些其他的选项,它们仍然使操作更简单,但让您可以控制它:

    1. 将许可证文本存储在某个地方,比如 ~/mitlicense :r ~/mitlicense ,这将把文件的内容插入到新文件中。如果要进一步加快速度,请映射一个执行此操作的命令,甚至为其指定一个键盘快捷键。
    2. 定义许可证的“缩写”( instructions here )。然后您只需键入一个简短的标识符,vim将用许可证替换该标识符。一些你不会偶然输入的东西,比如“#mit”可能会完成这项工作。
        4
  •  2
  •   Zsolt Botykai    16 年前

    http://www.google.hu/search?&q=vim+templating . 您还可以使用自动命令为每个新文件自动执行已经推荐的“:map”命令。您可以将其缩小到特定的文件类型。

        5
  •  2
  •   antoyo    11 年前

    我编写了一个插件,提供在缓冲区顶部插入许可证的命令。

    Here it is.

    autocmd BufNewFile * Gpl
    

    与其他答案相比,该插件的优势在于它支持多种编程语言(它将使用 comments 选项)。

    它还自动插入当前年度。