如何让 vim 为新文件添加标题注释?
我这些天写了很多 Rails 应用程序,并且想让 vim 添加标题注释到我正在处理的所有代码中。
我倾向于将我的项目存储在
~/Development/Repos/Personal
并且
~/Development/Repos/Work
我可以让 vim 根据文件的位置使用不同的版权等吗正在被创建?
I write a lot of Rails apps these days and would like to have vim add header comments to all the code I work on..
I tend to store my projects in
~/Development/Repos/Personal
And
~/Development/Repos/Work
Can I get vim to use different copyrights etc based on where abouts the file is being created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将标题模板保存为纯文本文件,然后使用 将其读入新文件:阅读。至于检查路径,只需编写一个 Ruby 脚本来生成所需的文本并使用 :阅读!。创建一个真正的 vim 插件也是一种选择。然而,当您已经知道如何在 Ruby 中处理文本和路径时,为什么还要浪费时间学习新语言和 API呢?不过,如果您熟悉 bash 脚本,那么它会产生更少的摩擦。
You can just save a header template as a plain text file and read it into a new file with :read. As for checking the path, just write a Ruby script to produce the desired text and invoke it with :read!. Creating a true vim plugin is also an option. However, why waste time learning a new language and API when you already know how to deal with text and paths in Ruby? Although, a bash script would create even less friction if you are comfortable with it.
我建议您使用众多代码片段插件之一,例如 XPTemplate 或snipMate,创建一个“标题”片段,然后使用它。这些插件的强大之处在于,您只需输入一个单词,然后按 Tab 即可获取扩展的代码片段。
I suggest you to use one of the many snippet plugins, like XPTemplate or snipMate, to create a 'header' snippet and then use it. The force of these plugins is that you just have to type a word and then press tab to get the expanded snippet.
这是我的 vimrc 中的一个片段,它在我创建文件时放入样板文件命名为 test_something.rb。您可以使用类似的 autocmd 有条件地添加您想要的版权。您可能需要检查函数中的扩展路径,但这似乎可以通过一些 vim 脚本来实现。
Here's a snippet from my vimrc which puts in boilerplate when I create a file named test_something.rb. You can probably use a similar autocmd to conditionally add the copyright you desire. You may have to check for the expanded path in the function, but it seems doable with some vimscripting.