首页 > 解决方案 > 如何在vim中新创建的文件中插入日期?

问题描述

我是 vim 的新手,我不知道要寻找什么。这就是我问这个问题的原因。

我搜索了互联网,发现它可以为特定文件名创建一个骨架文件。我有一个如下所示的文件:

/**
 *  author: 
 *  created: 
**/

创建新文件时,有什么方法可以自动执行以下操作。

/**
 *  author: mahfuzz
 *  created: 21.04.2020
**/

标签: datetimevim

解决方案


实际上,可以在这里看到某种动态模板

~/.config/nvim/after我有一个名为ultisnips_custom.vim

"             File: ultisnips_custom.vim - Custom UltiSnips settings
"       Maintainer: Sergio Araújo
" Oririnal Creator: Noah Frederick
"      Last Change: abr 16, 2020 - 14:50
"      Place it at: after/plugin/ultisnips_custom.vim

" We need python or python3 to run ultisnips
if !has("python") && !has("python3")
  finish
endif

" This function is called by the autocommand at the end of the file
function! TestAndLoadSkel() abort
  let filename = expand('%')
  " Abort on non-empty buffer or extant file
  if !(line('$') == 1 && getline('$') == '') || filereadable('%')
    return
  endif

  " Load UltiSnips in case it was deferred via vim-plug
  if !exists('g:did_plugin_ultisnips') && exists(':PlugStatus')
    call plug#load('ultisnips')
    doautocmd FileType
  endif

  " the function feedkys simulates the insert key sequence in order to call
  " the template (skel)
  execute 'call feedkeys("i_skel\<C-r>=UltiSnips#ExpandSnippet()\<CR>")'
endfunction

augroup ultisnips_custom
  autocmd!
  au Bufnewfile *.sh,*.zsh,*.html,*.css,*.py,*.tex,*.md,*.vim :call TestAndLoadSkel()
augroup END

" vim: fdm=marker:sw=2:sts=2:et

推荐阅读