neo-tree.nvim icon indicating copy to clipboard operation
neo-tree.nvim copied to clipboard

Navigate to parent directory of the current file

Open aakashsigdel opened this issue 2 years ago • 1 comments

Nerdtree had a pretty good feature where you could open nerdtree, focus on a file, then if you press "P", it would navigate to parent directory of the file and you would keep climbing the tree until you reached the root of the project.

In the example bellow, if you pressed "P" (or any other key mapped to this functionality) it would navigate to the "bye" directory, then again pressing "P" would navigate you to "hello" directory and so on.

Screen Shot 2022-09-20 at 9 08 21 pm

Is there a way to achieve this with neo-tree?

aakashsigdel avatar Sep 20 '22 11:09 aakashsigdel

You can do that with a custom command. This discussion has an example of a slightly more complicated version: https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/163

If you just want to go up one directory every time, you can do that with:

require("neo-tree").setup({
  window = {
    mappings = {
      ["P"] = function(state)
        local node = state.tree:get_node()
        require("neo-tree.ui.renderer").focus_node(state, node:get_parent_id())
      end
    }
  }
})

cseickel avatar Sep 20 '22 11:09 cseickel

@aakashsigdel does @cseickel comment resolve your issue? Think we can close this issue out?

miversen33 avatar Feb 04 '23 05:02 miversen33

Closing as solved.

nhat-vo avatar Apr 09 '23 13:04 nhat-vo

["P"] = functions

It should be function

FrankFang avatar Aug 07 '23 08:08 FrankFang

["P"] = functions

It should be function

I fixed the example @FrankFang

cseickel avatar Aug 07 '23 11:08 cseickel

How does this work for navigating up, from the parent directory?

Once you get to the top main directory, given where you opened nvim from - you can't go any further up 🤔

thewatts avatar Feb 21 '24 22:02 thewatts

I like the solution and implemented it but same question as @thewatts.

If already at the top of neotree I want to be able to navigate to the neotree parent folder. If I keep hitting "P" at the top of the neotree I SHOULD be able to eventually get to /

daxgames avatar Mar 05 '24 11:03 daxgames

Changing the root folder of neo-tree requires a lot of computation throughout the whole tree which is a heavy task compared to other operations.

I strongly do not recommend to have a keybind that may move cwd accidentally as, you know, you don't want to accidentally freeze neovim.

Instead please be patient and see that the redraw has finished instead of spamming the key when you are going to move the root.

I understand it may be inconvenient from time to time but I recommend to have a dedicated key to move the root just to be safe (by default it's <BS> mapped to "navigate_up").

pysan3 avatar Mar 05 '24 12:03 pysan3

@thewatts I didn't notice your comment :P

It's <BS> (backspace) by default mapped to "navigate_up"

pysan3 avatar Mar 05 '24 12:03 pysan3