Open a file and keep tree open, but another hotkey for open file and close the tree afterwards
alex-courtis opened this issue · comments
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>
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,
},
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 },
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.
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.
-
Suppose I have two files
foo1.txt
andfoo2.txt
inside my current working directory. Neovim is running, but no buffers are opened just yet.
-
Now, let's say I navigate the cursor to
foo1.txt
and hitTab
.foo1.txt
opens in a new buffer as expected.
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.
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 theTab
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.
This would be useful. Some IDEs have such a feature.
Indeed. Hopefully it will be implemented in the future.
Indeed. Hopefully it will be implemented in the future.
Pull requests are gratefully appreciated.