Skip to content

Open a file and keep tree open, but another hotkey for open file and close the tree afterwards #1984

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

Closed
alex-courtis opened this issue Feb 11, 2023 Discussed in #1980 · 12 comments · Fixed by #3054
Closed
Labels
API API or Event feature request PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated

Comments

@alex-courtis
Copy link
Member

Discussed in #1980

Originally posted by mangelozzi February 9, 2023
I found there is an option: nvim-tree.actions.open_file.quit_on_open, but the problem with this it always closes the tree afterwards.
I wish to have o perform as default (keep the tree open after opening the file), but have <CR> open the buffer and close the tree.

Currently these seems to be the only options (tried them all):

`<CR>`            edit                open a file or folder; root will cd to the above directory
`o`
`<2-LeftMouse>`
`<C-e>`           edit_in_place       edit the file in place, effectively replacing the tree explorer
`O`               edit_no_picker      same as (edit) with no window picker

edit_in_place is similar but then I have 2 windows after opening the file, and have to go to the other window and close it.

Looking for something like this:

require("nvim-tree").setup({
    view = {
        mappings = {
            list = {
                { key = "<CR>", action = "quit_on_open" },
            },
        },
    },    
})
````</div>
@alex-courtis
Copy link
Member Author

This is not currently possible. It could be done via API via a default nil option:

          {
            key = "o",
            action = "edit_no_close",
            action_cb = function(node)
              api.node.open.edit(node, {
                close_tree = false,
              })
            end,
          },

@mangelozzi
Copy link

Thanks for this, I think it would help others.
For me I used your suggestion (but reversed the logic so its closer to defaults), the only thing is I found close_tree=true did not work for me, I have to call api.tree.close()

-- Use `o` to open a file
-- Use `<CR>` to open a file and close the tree

local api = require("nvim-tree.api")

local function edit_and_close(node)
    api.node.open.edit(node, { close_tree = true, })
    api.tree.close()
end

--  { key = "<CR>", action = "edit_and_close", action_cb=edit_and_close },

@alex-courtis
Copy link
Member Author

For me I used your suggestion (but reversed the logic so its closer to defaults), the only thing is I found close_tree=true did not work for me, I have to call api.tree.close()

Yes. That has not actually been built. This is a feature request with a suggested action.

I'm glad you found something that works in the meantime.

@anthony-S93
Copy link

anthony-S93 commented Feb 18, 2023

The closest thing I can find to achieve this behavior (without having to make api calls as suggested above) is via the "preview" feature mentioned in the documentation under the "default mappings" section; that is, if we press the Tab key on a file, a buffer will open up, but nvim-tree will continue to receive focus even after the buffer is open.

The only problem I have with this is that it's just a "preview" and not an actual opening of files. Meaning, the previously opened buffers via the Tab key will simply disappear if I use the Tab key to "open" another file.

Here is a simple scenario (with screenshots) to illustrate this behavior.

  1. Suppose I have two files foo1.txt and foo2.txt inside my current working directory. Neovim is running, but no buffers are opened just yet.
    2023-02-18-035736_721x374_scrot

  2. Now, let's say I navigate the cursor to foo1.txt and hit Tab. foo1.txt opens in a new buffer as expected.
    2023-02-18-035947_734x370_scrot

  3. Now, navigate the cursor to foo2.txt instead and hit Tab.
    2023-02-18-040035_661x316_scrot

What happens is that although foo2.txt opens as expected, but foo1.txt will not remain open.

It would be nice to have the option to keep nvim-tree in focus every time we use the Enter key to open a file. This feature will be particularly helpful to those of us whose main workflow with nvim-tree comprises of these steps:
1. Toggle nvim-tree with a keybinding
2. Browse through the directory structure with nvim-tree
3. Hit Enter to open desired file.
4. Continue navigating through nvim-tree to look for another file to open. (This, of course, assumes that quit_on_open is set to false)
5. Close nvim-tree with a keybinding after all the desired files are launched.

@alex-courtis
Copy link
Member Author

The only problem I have with this is that it's just a "preview" and not an actual opening of files. Meaning, the previously opened buffers via the Tab key will simply disappear if I use the Tab key to "open" another file.

Yes. Preview buffers are not particularly useful in this case, and not a solution.

It would be nice to have the option to keep nvim-tree in focus every time we use the Enter key to open a file.

This would be useful. Some IDEs have such a feature.

@anthony-S93
Copy link

This would be useful. Some IDEs have such a feature.

Indeed. Hopefully it will be implemented in the future.

@alex-courtis
Copy link
Member Author

Indeed. Hopefully it will be implemented in the future.

Pull requests are gratefully appreciated.

@alex-courtis alex-courtis added the PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated label Apr 11, 2023
@GCrispino
Copy link
Collaborator

Hey @alex-courtis , I was looking through this issue and unless I didn't follow it correctly, we could have two new options to the api.node.open.edit API method:

  • close_tree: what was originally suggested in this issue - which, if set to true, would close the tree when file related to the node is opened;
  • keep_tree_focus (or something like that): what has been suggested by @anthony-S93 - which, if set to true, would open the file but keep the focus on the tree. Similar to what's done in the preview feature, but actually creating a new buffer for each file

Does that sound reasonably right to you? If it does, do you think we could tackle both of this in a single PR? I think it makes sense since it would be two options added to the same method

@alex-courtis
Copy link
Member Author

It's great that we're extending API instead of using more global options; they are out of control.

Does that sound reasonably right to you? If it does, do you think we could tackle both of this in a single PR? I think it makes sense since it would be two options added to the same method

Sounds good; add the whole new opts in one go.

close_tree: what was originally suggested in this issue - which, if set to true, would close the tree when file related to the node is opened;

We might call it quit_on_open to match the global option nvim-tree.actions.open_file.quit_on_open, noting that this new API option overrides the global option.

keep_tree_focus (or something like that): what has been suggested by @anthony-S93 - which, if set to true, would open the file but keep the focus on the tree. Similar to what's done in the preview feature, but actually creating a new buffer for each file

How about just focus with a default of true? A bit shorter, and there's no global option name we need to follow.

@GCrispino
Copy link
Collaborator

Hey @alex-courtis , this sounds good to me - I've created a WIP PR to implement the first option only for now. I've added more details on its first comment. Would you mind having a look to check if you agree with how I did it? If that's fine I'll look into doing a similar thing for the focus option

@alex-courtis
Copy link
Member Author

I've created a WIP PR to implement the first option only for now.

One PR or two... whatever works best for you.

@GCrispino
Copy link
Collaborator

I've created a WIP PR to implement the first option only for now.

One PR or two... whatever works best for you.

Sorry, I didn't mean about whether to create one or two MRs, but about how I've currently implemented the quit_on_open functionality in the PR I linked, that is, if the way I did it looks good to you 🙏🏼

alex-courtis added a commit that referenced this issue Feb 22, 2025
… functions (#3054)

* feat: add quit_on_open opt to api.node.open.edit

* fix: fix missing @param annotation

* feat: add focus opt to api.node.open.edit

* fix: fix focus == false behaviour on api.node.open.tab command

* fix: add optional tabpage integer parameter to view.close

if tabpage is not nil, then the function closes the tabpage in this
specific tabpage

* fix: fix quit_on_open == true behaviour on api.node.open.tab command

* fix: add check to not use new opts for certain edit modes

* fix: add docs for new opts

---------

Co-authored-by: Alexander Courtis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API or Event feature request PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated
Projects
None yet
4 participants