Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with NERDTree #17

Open
hectorqiuiscool opened this issue Feb 3, 2014 · 9 comments
Open

Integrate with NERDTree #17

hectorqiuiscool opened this issue Feb 3, 2014 · 9 comments

Comments

@hectorqiuiscool
Copy link

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

@aperezdc
Copy link
Owner

aperezdc commented Feb 3, 2014

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.

@aperezdc aperezdc self-assigned this Feb 3, 2014
@koddsson
Copy link

Any news on this?

@aperezdc
Copy link
Owner

@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
Copy link

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
Copy link
Owner

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

@koddsson
Copy link

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
Copy link

gubei commented Jul 17, 2016

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
Copy link
Owner

aperezdc commented Sep 11, 2016

@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
Copy link

fx-kirin commented Oct 4, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants