开发者

How can you get vim to add a header comment to new files?

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 c开发者_StackOverflow社区reated?


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.


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.


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.

" Autocommands
autocmd BufNewFile *test*.rb call MakeRubyUnitTester()

"
" Functions
" Fill in the boilerplate for Ruby Unit Tests
function! MakeRubyUnitTester()
    exec "normal irequire 'test/unit'

class TC_Simple < Test::Unit::TestCase"
endfunction
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜