【问题标题】:define default text for vim editor为 vim 编辑器定义默认文本
【发布时间】:2021-03-05 10:12:31
【问题描述】:

我希望 vim 可以选择每当我使用 vim 创建新的 c++ 文件时,它会自动将我的基本代码文本写入其中。如:

#include<bits/stdc++.h>

using namespace std;
int main(){
}

请帮忙! 对不起,因为我的英语不好

【问题讨论】:

标签: vim


【解决方案1】:

Noah Frederick 写了an excellent article 关于如何使用 ultisnips 创建“骨架”文件:

基本上,他的脚本会检测何时创建文件并触发预定义的 sn-p。当然你还得安装ultisnips

有人可能会问:为什么不创建文件模板并通过自动命令触发它?答案很简单,ultisnips 解决方案允许您按需更改模板,它不仅仅是一个静态模板,you can seeNoah 解释了他的解决方案背后的想法。

"             File: ultisnips_custom.vim - Custom UltiSnips settings
"       Maintainer: Sergio Araújo
" Oririnal Creator: Noah Frederick
"        Reference: https://noahfrederick.com/log/vim-templates-with-ultisnips-and-projectionist
"      Last Change: out 11, 2020 - 17:18
"      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
" remember to create a _skel snippet with your header for each file you
" want a autosnippet 

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

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

【讨论】:

    【解决方案2】:

    只需将该文件作为 vim 模板保存到 $HOME/.vim/templates/cc.tpl。每当您编辑一个名为 foo.cc 的新文件时——瞧!

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2011-06-20
      • 2013-06-18
      • 2015-03-23
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多