How can I automatically add some skeleton code when creating a new file with vim

When creating a new file with vim, I would like to automatically add some skeleton code.

For example, when creating a new xml file, I would like to add the first line:

  <?xml version="1.0"?>

Or when creating an html file, I would like to add:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title></title>
  </head>
  <body>
  </body>
</html>

I got something like this in my .vimrc:

au BufNewFile *.xml 0r ~/.vim/xml.skel | let IndentStyle = "xml"
au BufNewFile *.html 0r ~/.vim/html.skel | let IndentStyle = "html"

And so on, whatever you'll need.


You can save your skeleton/template to a file, for example ~/vim/skeleton.xml

Then add the following to your .vimrc

augroup Xml
    au BufNewFile *.xml 0r ~/vim/skeleton.xml
augroup end

Sorry for the lateness, but I feel the way I do it might be useful to some. It uses the file's filetype, making it shorter and more dynamic than more conventional methods. It was tested only on Vim 7.3.

if has("win32") || has ('win64')
    let $VIMHOME = $HOME."/vimfiles/"
else
    let $VIMHOME = $HOME."/.vim/"
endif

" add templates in templates/ using filetype as file name
au BufNewFile * :silent! exec ":0r ".$VIMHOME."templates/".&ft

If you want to adapt your skeleton to the context, or to the user choices, have a look at the template-expander plugins listed on vim.wikia