Nerdtree: Create file (m ↦ a) does not trigger BufNewFile

Created on 11 Sep 2016  ·  1Comment  ·  Source: preservim/nerdtree

When using the “Create file” method (keys ma), it seems that NERDtree first creates an empty file, and then opens the empty file. This creates and opens the file in Vim _without_ triggering the BufNewFile event.

In particular, this breaks using the vim-templates plugin, please check the discussion on aperezdc/vim-template#17 for more details.

Most helpful comment

@aperezdc, the fs_menu.vim NERDTree plugin is meant to be analogous to the file operations one would do by right-clicking in Windows Explorer. The difference here is between creating a file on disk first, and creating an empty buffer directly in vim. The latter triggers BufNewFile; whereas, opening a file will fire the BufReadPost. I took a look at the code for vim-template's TLoad function. It looks like it's already doing a check to make sure the buffer is empty. What happens if you modify the autocmd to trigger on BufReadPost also, like this?

if !g:templates_no_autocmd
    augroup Templating
        autocmd!
        autocmd BufNewFile,BufReadPost * call <SID>TLoad()
    augroup END
endif

The other option, as you mentioned is to write your own function using NERDTree's API.

>All comments

@aperezdc, the fs_menu.vim NERDTree plugin is meant to be analogous to the file operations one would do by right-clicking in Windows Explorer. The difference here is between creating a file on disk first, and creating an empty buffer directly in vim. The latter triggers BufNewFile; whereas, opening a file will fire the BufReadPost. I took a look at the code for vim-template's TLoad function. It looks like it's already doing a check to make sure the buffer is empty. What happens if you modify the autocmd to trigger on BufReadPost also, like this?

if !g:templates_no_autocmd
    augroup Templating
        autocmd!
        autocmd BufNewFile,BufReadPost * call <SID>TLoad()
    augroup END
endif

The other option, as you mentioned is to write your own function using NERDTree's API.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MuhammadMouradG picture MuhammadMouradG  ·  6Comments

yuanjianpeng picture yuanjianpeng  ·  5Comments

kristijanhusak picture kristijanhusak  ·  3Comments

NikosEfthias picture NikosEfthias  ·  3Comments

sunnyguan picture sunnyguan  ·  4Comments