2

I have different templates in this directory ~/Documents/Library/
like: Template.cpp, normal.cpp, advanced.cpp

Normally I use this:

autocmd BufNewFile *.cpp 0r ~/Documents/Library/Template.cpp

I want to add an option "choose template" for new files that I create via vim file.cpp or vim -p file1.cpp file2.cpp

  1. After triggering vim file.cpp, Vim will show me multiple options. After choosing one, Vim will use that template.

  2. In case of vim -p file1.cpp file2.cpp, I want to choose only one for all tabs. Vim will use that template on all tabs.

Is it possible?
if yes, then please help me make this config :)

1
  • 3
    Well, there's input or popup_menu for making the selection. Then you have to handle argv() or v:argv (to detect -p) in some kind of VimEnter autocommand to decide what to do
    – D. Ben Knoble
    Sep 15, 2021 at 19:20

2 Answers 2

1

A rough sketch with lots of bugs, probably. The function names could use some work, too.

function Templates(ext) abort
  return glob(printf('~/Documents/Library/**/*.%s', a:ext), v:false, v:true)
endfunction

function ChooseTemplateAll() abort
  const ext = fnamemodify(@%, ':e')
  call popup_menu(Templates(ext), #{callback: 'SelectedTemplate', title: 'template for '.@%})
endfunction

function SelectedTemplate(id, result) abort
  if a:result is# -1
    " didn't choose anything
  else
    execute '0read' Templates(fnamemodify(@%, ':e'))[a:result]
  endif
  next
  call ChooseTemplateAll()
endfunction

function ChooseOneTemplate() abort
  const ext = fnamemodify(@%, ':e')
  call popup_menu(Templates(ext), #{callback: 'SelectedOneTemplate', title: 'template for all files'})
endfunction

function SelectedOneTemplate(id, result) abort
  if a:result is# -1
    " didn't choose anything
  else
    execute 'argdo 0read' Templates(fnamemodify(@%, ':e'))[a:result]
    first
  endif
endfunction

function TryTemplate() abort
  if len(v:argv) >= 2 && index(v:argv, '-p') >= 0
    call ChooseOneTemplate()
  else
    call ChooseTemplateAll()
    rewind
  endif
endfunction

augroup templates
  autocmd!
  autocmd VimEnter * call TryTemplate()
augroup END
14
  • Could you please make the script foolproof?
    – Mega Bang
    Jan 2, 2023 at 14:00
  • @MegaBang could you please specify what is wrong and what needs improved? It was intended as a sketch for you to adapt to your needs.
    – D. Ben Knoble
    Jan 3, 2023 at 14:07
  • I'm still a noob at this. sorry
    – Mega Bang
    Jan 3, 2023 at 14:45
  • Are you there @d-ben-knoble
    – Mega Bang
    Mar 23, 2023 at 7:19
  • 1
    @MegaBang what does "not working as expected" mean? Details would be the most helpful.
    – D. Ben Knoble
    Mar 27, 2023 at 15:11
0

If you're running GVim you can do

:bro r ~/Documents/Library

and choose file using the standard GUI dialog.

1
  • used to run GVim on windows, now do all the work in terminal via NeoVim
    – Mega Bang
    Apr 1, 2022 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.