How to use a template in vim

前端 未结 7 1976
一个人的身影
一个人的身影 2020-12-08 02:40

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,

7条回答
  •  时光说笑
    2020-12-08 03:28

    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(":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

提交回复
热议问题