vim-template
vim-template copied to clipboard
Integrate with NERDTree
Hi Adrian, could I make it work with NERDTree's create file method(keys: m -> a)? https://github.com/scrooloose/nerdtree
I have never used NERDTree myself. I will need to have a look to be sure... on a quick look it seems that it would be doable.
Any news on this?
@koddsson Not for now... but I have been taking a quick look at the NERDTree documentation, and I see there is some API exposed by NERDTree which we could use to add items to its menus. Better than modifying the behaviour of m
↦a
, I think it would be better to add a new item to the menu using NERDTreeAddMenuItem()
. Something like the following to create an entry which can be triggered using the m
↦A
key sequence:
function EditNewFilename()
let l:filename = input("filename: ")
execute "edit " . l:filename
end
call NERDTreeAddMenuItem({
\ 'text': 'New with templ(A)te',
\ 'shortcut': 'A',
\ 'callback: 'EditNewFilename' })
If the templates plugin is installed and using vim <filename>
from the command line inserts a template automatically for you, the above should work when added to ~/.vimrc
, because using execute "edit <filename>"
triggers the autocommand which searches for templates in the same way.
Actually, being this easy to add your own “new file with template” item to the NERDTree menu, I think it could be better to add the example in the documentation, instead of explicitly adding support in the templates plugin, and letting people add the snipped in their ~/.vimrc
instead. WDYT?
That sounds good to me! I've never touched on vimscript before so I had some trouble setting this in my vimrc
but I got it (sorta) working :)
@koddsson Glad to hear it worked, because the code I have posted before was completely untested. Of course I will test properly the version that finally will go into the documentation :wink:
Only thing that needs to change is that the snippet will open the file up in the current buffer (ie. the NERDTree window). I'm hoping to get a chance to look at the API and see if I can open it up in the "main" buffer.
I explore the answer why nerdtree add new file that do not trigger vim-template .The reason is the way which nerdtree using that touch a new file using system call , so it does not trigger BufNewFile event . above answer I not statisfied me . I use another way to solve it . here is my code.
function! GetCurrentContent()
let l:content = getline(0,line("$"))
let l:result = 0
for l:temp in l:content
if strlen(l:temp)> 0
let l:result = 1
break
endif
endfor
if l:result == 0
let l:extension = expand("%:c")
exe 'Template .' . l:extension
endif
endfunction
autocmd BufEnter * call GetCurrentContent()
@gubei: Hey, that's an interesting find, thanks for sharing! I wonder if maybe this is a bug in NERDtree, because in my opinion creating a file from NERDtree should work in a way that the BufNewFile
event is actually triggered.
I have reported the issue in the NERDtree projects, let's see how that goes.
function EditNewFilename()
let l:filename = input("filename: ")
exec ':wincmd l'
exec "edit " . l:filename
endfunction
call NERDTreeAddMenuItem({
\ 'text': '(t)New with template',
\ 'shortcut': 't',
\ 'callback' : 'EditNewFilename' })
For me, the code above is enough to solve the problem.