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

后端 未结 8 1414
清歌不尽
清歌不尽 2020-11-29 02:42

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 lin

8条回答
  •  离开以前
    2020-11-29 02:55

    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
    

提交回复
热议问题