25

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 template

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 ;-/

Hotschke
  • 9,402
  • 6
  • 46
  • 53
Andreas
  • 6,612
  • 14
  • 59
  • 69
  • Is it just me or is there a blank line added before the template everytime on the creation of a new file? Anyway to rid of the newline? – Kevin Lee Dec 31 '13 at 18:45

7 Answers7

31

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.

DrAl
  • 70,428
  • 10
  • 106
  • 108
  • Beauti-forking-full - very good answer - walking me through the steps. Thanks alot. For now the crude method works for what I had intended, but I have downloaded the extension, and will see how I can use it :-) - thanks. – Andreas Sep 24 '10 at 11:10
  • 6
    Use `0r` instead of `r` and the insertion will not add in a blank line. – Kevin Lee Jan 01 '14 at 22:59
10

Assume that your skeletons are in your ~/.vim/templates/ directory, you can put this snippet in your vimrc file.

augroup templates
  au!
  " read in templates files
  autocmd BufNewFile *.* silent! execute '0r ~/.vim/templates/skeleton.'.expand("<afile>:e")
augroup END

Some explanation,

  • BufNewFile . = each time we edit a new file
  • silent! execute = execute silently, no error messages if failed
  • 0r = read file and insert content at top (0) in the new file
  • expand(":e") = get extension of current filename

see also http://vim.wikia.com/wiki/Use_eval_to_create_dynamic_templates

*fixed missing dot in file path

Community
  • 1
  • 1
guest
  • 101
  • 1
  • 2
7

Create a templates subdirectory in your ~/.vim folder

$ mkdir -p ~/.vim/templates

Create a new file in subdirectory called R.skeleton and put in the header and/or other stuff you want to automagically load upon creating a new ".R " file.

$ vim ~/.vim/templates/R.skeleton

Then, add the following to your ~/.vimrc file, which may have been suggested in a way by "guest"

autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.skeleton

Have a look at my github repository for some more details and other options.

ILMostro_7
  • 1,422
  • 20
  • 28
2

It's just a trick I used to use . It's cheap but If you ain't know nothing about vim and it's commands it's easy to handle. make a directory like this :

~/.vim/templates/barney.cpp

and as you konw barney.cpp should be your template code . then add a function like ForUncleBarney() to end of your .vimrc file located in ~/.vimrc it should be like

function ForBarneyStinson()
   :read ~/.vim/templates/barney.cpp
endfunction

then just use this command in vim

:call ForBarneyStinson()

then you see your template as an example I already have two templates for .cpp files

:call ForBarney()
:call ACM()

sorry said too much, Coding's awesome ! :)

2

Also take a look at https://github.com/aperezdc/vim-template.git.

I use it and have contributed some patches to it and would argue its relatively full featured.

zanegray
  • 768
  • 7
  • 13
1

There exist many template-file expanders -- you'll also find there explanations on how to implement a rudimentary template-file expander.

For my part, I'm maintaining the fork of muTemplate. For a simple start, just drop a {ft}.template file into {rtp}/template/. If you want to use any (viml) variable or expression, just do. You can even put vim code (and now even functions) into the template-file if you wish. Several smart decisions are already implemented for C++ and vim files.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
1

What about using the snipmate plugin? See here

Benoit
  • 76,634
  • 23
  • 210
  • 236