Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

r - How to use a template in vim

This is really a newbie question - but basically, how do I enable a template for certain filetypes.

Basically, I just want the template to insert a header of sorts, that is with some functions that I find useful, and libraries loaded etc.

I interpret

:help templates

the way that I should place this in my vimrc

au BufNewFile,BufRead ~/.vim/skeleton.R

Running a R script then shows that something could happen, but apparently does not:

--- Auto-Commands ---

This may be because a template consists of commands (and there are no such in skeleton.R) - and in this case I just want it to insert a text header (which skelton.R consist of).

Sorry if this question is mind boggeling stupid ;-/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The command that you've suggested is not going to work: what this will do is run no Vim command whenever you open ~/.vim/skeleton.R

A crude way of achieving what you want would be to use:

:au BufNewFile *.R r ~/.vim/skeleton.R

This will read (:r) your file whenever a new *.R file is created. You want to avoid having BufRead in the autocmd, or it will read the skeleton file into your working file every time you open the file!

There are many plugins that add a lot more control to this process. Being the author and therefore completely biased, I'd recommend this one, but there are plenty of others listed here.


Shameless plug:

They all work in a relatively similar way, but to explain my script:

You install the plugin as described on the linked page and then create some templates in ~/.vim/templates. These templates should have the same extension as the 'target' file, so if it's a template for .R files, call it something like skeleton.R. In your .vimrc, add something like this:

let g:file_template_default = {}
let g:file_template_default['R'] = 'skeleton'

Then create your new .R file (with a filename, so save it if it's new) and enter:

:LoadFileTemplate

You can also skip the .vimrc editing and just do:

:LoadFileTemplate skeleton

See the website for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...