Skip to content

Instantly share code, notes, and snippets.

@edsono
Created June 16, 2011 19:53
Show Gist options
  • Save edsono/1030101 to your computer and use it in GitHub Desktop.
Save edsono/1030101 to your computer and use it in GitHub Desktop.
Create new files from template
" Vim global plugin for creating files from templates
" Last Change: 15/06/2011
" Maintainer: Edson César <[email protected]>
" License: This file is placed in the public domain.
function! s:Skel(template, filename)
if !empty(fnamemodify(a:filename, ":e"))
let l:fn = a:filename
else
let l:fn = a:filename . "." . fnamemodify(a:template, ":e")
endif
exec "new " . l:fn
exec "0r ~/.vim/skel/" . a:template
exec "%s/\$filename/" . fnamemodify(l:fn, ":t:r") . "/g"
endfunction
function! s:ListTemplates(ArgLead, CmdLine, CursorPos)
if empty(a:ArgLead)
let l:search = "**/*.*"
else
let l:search = "**/" . a:ArgLead . "*.*"
endif
let l:result = []
for fn in split(globpath("~/.vim/skel", l:search), "\n")
call insert(l:result, fnamemodify(fn, ":t"))
endfor
return l:result
endfunction
command! -complete=customlist,<SID>ListTemplates -nargs=+ New call <SID>Skel(<f-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment