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

8

35

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>
Couchant answered 2/10, 2008 at 14:33 Comment(0)
B
5

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

Baran answered 2/10, 2008 at 15:0 Comment(0)
M
29

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.

Modern answered 2/10, 2008 at 14:40 Comment(0)
R
16

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
Resorcinol answered 2/10, 2008 at 14:46 Comment(2)
What is the meaning of 0r here?Hardboard
@crison on the meaning of 0r. 0 positions the insertion to line zero and the r instructs vim to read in the contents of the file at the insertion location.Resorcinol
F
8

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
Fritter answered 31/8, 2011 at 23:55 Comment(1)
@Fritter - I just replaced the config I've been using for a long time with your filetype method. Much cleaner. Found it useful to list what the filetypes were after this. In vim if you execute :echo glob($VIMRUNTIME . '/ftplugin/*.vim') or :echo glob($VIMRUNTIME . '/syntax/*.vim') it will list all of the types vim knows about.Irisirisa
B
5

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

Baran answered 2/10, 2008 at 15:0 Comment(0)
D
1

Here are two examples using python scripting.

Add something like this in your .vimrc or another file sourced by your .vimrc:

augroup Xml
  au BufNewFile *.xml :python import vim
  au BufNewFile *.xml :python vim.current.buffer[0:0] = ['<?xml version="1.0"?>']
  au BufNewFile *.xml :python del vim
augroup END

fu s:InsertHtmlSkeleton()
  python import vim
  python vim.current.buffer[0:0] = ['<!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>"]
  python del vim
endfu

augroup Html
  au BufNewFile *.html call <SID>InsertHtmlSkeleton()
augroup END
Dallas answered 2/10, 2008 at 14:38 Comment(0)
U
1

You can add various hooks when files are read or created. to

:help event

and read what's there. What you want is

:help BufNewFile
Ungenerous answered 2/10, 2008 at 14:39 Comment(0)
B
0

It can work with snipmate too:

augroup documentation
    au!
    au BufNewFile *.py :call ExecuteSnippet('docs')
augroup END

function! ExecuteSnippet(name)
    execute "normal! i" . a:name . "\<c-r>=TriggerSnippet()\<cr>"
endfunction

with "docs" the snippet to trigger.

It works with multi-snippets but then the :messages window appears and it's cumbersome.

Budweis answered 13/11, 2014 at 13:40 Comment(0)
W
0

I wrote a plugin for html:

On vim scripts: http://www.vim.org/scripts/script.php?script_id=4845

On Github: https://github.com/linuscl/vim-htmltemplate

Wharf answered 3/5, 2015 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.