写了一个vim 插件, nskeleton.vim

写了一个vim插件. 做两件事情:默认的skeleton, 更新last_update

skeleton 其实很简单:

autocmd BufNewFile *.py 0r ~/.vim/skeleton/py_skeleton.py

但是想插入一个自动的Date, author之类的字段, 搜了一下, 有现成的tskeleton: 但是居然有1万来行代码,我了个去,试用了一把,居然还有bug!!!!不能忍了,于是自己写了一个

结果满足自己的需求,代码只有30行左右:

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

function! Replace()
    let n = min([20, line("$")])
    keepjumps exe '1,' . n . 's#<+DATE+>#\1' .  strftime('%F %T') . '#e'
    "Decho "debug message " . &ft. expand('%:t')
    keepjumps exe '1,' . n . 's#<+FILE_NAME+>#' .  expand('%:t') . '#e'
    "call histdel('search', -1)
endfun

au BufNewFile * :silent! exec ":0r ".$VIMHOME."skeletons/". &ft . ".skeleton" | call Replace()



function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([20, line("$")])
    keepjumps exe '1,' . n . 's#^\(.\{,10}Last modified: \)\S* \S*#\1' .
          \ strftime('%F %T') . '#e'

    "Decho "debug message " 

    "call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun

autocmd BufWritePre * call LastModified()

你可能感兴趣的:(vim)