aperezdc / vim-template

Simple templates plugin for Vim
370 stars 152 forks source link

Integrate with NERDTree #17

Open hectorqiuiscool opened 10 years ago

hectorqiuiscool commented 10 years ago

Hi Adrian, could I make it work with NERDTree's create file method(keys: m -> a)? https://github.com/scrooloose/nerdtree

aperezdc commented 10 years ago

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.

koddsson commented 8 years ago

Any news on this?

aperezdc commented 8 years ago

@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 ma, 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 mA 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?

koddsson commented 8 years ago

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 :)

aperezdc commented 8 years ago

@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:

koddsson commented 8 years ago

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.

gubei commented 8 years ago

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()

aperezdc commented 8 years ago

@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.

fx-kirin commented 6 years ago
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.