当我使用vim创建一个.tex文件时,我得到了一个不错的模板

autocmd BufNewFile *.tex 0r $HOME/.vim/templates/skeleton.tex

在我的.vimrc中。我的主目录中也有一个makefile-template,但是我必须手动将其复制到.tex文件所在的位置。在Linux环境中,如何在创建.tex文件的同时自动复制或自动生成makefile?

最佳答案

可移植的答案不会使用cp(它可能会覆盖先前存在的makefile),而是vim函数readfile()和writefile()。

要触发它,最好的办法是定义并执行一个函数,该函数加载第一个骨架,并动态创建Makefile:

" untested code
"
function! s:NewTeXPlusMakefile()
  let where = expand('%:p:h')
  " see mu-template's fork for a more advanced way to find template-files
  let skeletons_path = globpath(&rtp, 'templates')

  " the current .tex file
  let lines = readfile(skeletons_path.'/skeleton.tex')
  call setline(1, lines)

  " the Makefile, that MUST not be overwritten when it already exists!
  let makefile = where.'/Makefile'
  if ! filereadable(makefile)
    let lines = readfile(skeletons_path.'/skeleton.makefile')
    call writefile(lines, makefile )
  endif
endfunction

augroup LaTeXTemplates
  au!
  au BufNewFile *.tex call s:NewTeXPlusMakefile()
augroup END

10-07 19:15
查看更多