There is a nice example of how to open neotree in the project root
see: https://www.emacswiki.org/emacs/NeoTree (find-file-in-project and Projectile sections).

However, I'd like to toggle neotree, so I can easily open and close it, while having the root set to the project path.

Is this possible?


Using a slightly modified version of this post, I managed to get this working:

(defun my-neotree-project-dir-toggle ()
  "Open NeoTree using the project root, using projectile, find-file-in-project,
or the current buffer directory."
  (require 'neotree)
  (let* ((filepath (buffer-file-name))
               ((featurep 'projectile)
               ((featurep 'find-file-in-project)
               (t ;; Fall back to version control root.
                (if filepath
                     (vc-responsible-backend filepath) 'root filepath)
         (neo-smart-open t))

    (if (and (fboundp 'neo-global--window-exists-p)
      (when project-dir
        (neotree-dir project-dir))
      (when filepath
        (neotree-find filepath)))))

Key binding to toggle, example:

(define-key global-map (kbd "M-e") 'my-neotree-project-dir-toggle)

It's now 2020 and you can open neotree in the current directory using neotree-show instead of neotree-toggle.

neotree-show does exactly what the orignal op required, without the need for the elaborate function.

  • Thanks I tried this but found this still not quite as good as neotree-toggle snippet. As it doesn't show a tree for the paths leading up to the current file. Also, looking at the code, it doesn't support find-file-in-project or vc methods of finding the root. – ideasman42 Apr 5 '20 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.