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

:NERDTreeFind for newly created and yet unsaved files #1042

Closed
mgedmin opened this issue Sep 27, 2019 · 2 comments · Fixed by #1043
Closed

:NERDTreeFind for newly created and yet unsaved files #1042

mgedmin opened this issue Sep 27, 2019 · 2 comments · Fixed by #1043

Comments

@mgedmin
Copy link

mgedmin commented Sep 27, 2019

Description

It would be rad if :NERDTreeFind would open NERDTree and focus on the parent directory when I run it in a buffer that has a new file that doesn't yet exist on disk.

Elaboration

I have several custom commands :EditFiletypePlugin, :EditSyntaxPlugin, :EditIndentPlugin that open my local ~/.vim/{ftplugin,syntax,indent}/&ft.vim. Sometimes there's no local plugin for this filetype, but vim's global one includes a different (like syntax/git.vim includes syntax/diff.vim), and I have a local override for that one.

So when I :EditSyntaxPlugin and I get a new file ~/.vim/syntax/git.vim, but I want to instead edit ~/.vim/syntax/diff.vim (which exists), I try to use :NERDTreeFind so I could look at the files in the same folder.

Unfortunately :NERDTreeFind tells me "NERDTree: invalid path".

I could do :NERDTreeFind %:h, but

  • I have a mapping f for :NERDTreeFind, and I'd have to come up with a different key for the :%h variation (or construct elaborate mappings that check for the existence of the file, hmmm)
  • I would like NERDTree to also open the directory and show me what's inside, which the %:h version doesn't currently do
@mgedmin
Copy link
Author

mgedmin commented Sep 30, 2019

Thank you!

@sou-long
Copy link

sou-long commented Dec 2, 2023

If file path doesn't exist, :NERDTreeFind its parent directory instead

This still errs on empty unnamed buffers (new tabs, diffs, etc). I wanted NERDTreeFind to fallback to NERDTree in any case, so I came up with this snippet:

nmap <mykey> :call NERDTreeFindOrOpen()<CR>

function! NERDTreeFindOrOpen()
    if &modifiable && !empty(expand('%:p'))
        NERDTreeFind
    else
        NERDTree
    endif
endfunction
  • &modifiable makes it ignore help/etc buffer paths
  • %:p means file path
  • You may experiment with NERDTreeFocus or NERDTreeVCS instead of NERDTree

Feel free to adjust and use!

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

Successfully merging a pull request may close this issue.

2 participants