如何在vim中使用模板 [英] 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.
我解释
:help templates
我应该把它放在我的 vimrc 中的方式
the way that I should place this in my vimrc
au BufNewFile,BufRead ~/.vim/skeleton.R
然后运行 R 脚本表明可能会发生某些事情,但显然不会:
Running a R script then shows that something could happen, but apparently does not:
--- Auto-Commands ---
这可能是因为模板由命令组成(在 skeleton.R 中没有这样的命令) - 在这种情况下,我只想插入一个文本标题(由 skelton.R 组成).
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 ;-/
推荐答案
您建议的命令不起作用:每当您打开 ~/.vim/时,这将执行 no Vim 命令骨架.R
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
每当创建新的 *.R
文件时,它都会读取 (:r
) 您的文件.您希望避免在 autocmd 中使用 BufRead
,否则每次打开文件时它都会将骨架文件读入您的工作文件!
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.
无耻的插头:
它们都以相对相似的方式工作,但为了解释我的脚本:
They all work in a relatively similar way, but to explain my script:
您按照 linked 页面上的说明安装插件,然后在 ~/.vim/templates
中创建一些模板.这些模板应该与目标"文件具有相同的扩展名,因此如果它是 .R
文件的模板,则将其命名为 skeleton.R
.在您的 .vimrc
中,添加如下内容:
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'
然后创建新的 .R
文件(带有文件名,如果是新文件,请保存)并输入:
Then create your new .R
file (with a filename, so save it if it's new) and enter:
:LoadFileTemplate
你也可以跳过 .vimrc
编辑,直接做:
You can also skip the .vimrc
editing and just do:
:LoadFileTemplate skeleton
有关详细信息,请参阅网站.
See the website for more details.
这篇关于如何在vim中使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!