-
Notifications
You must be signed in to change notification settings - Fork 258
Recipes
Welcome to the neo-tree.nvim wiki!
This is a place to share configuration recipes for functionality that others may want to copy or learn from.
Sometimes you may want to add a filter and leave that way because the filter captures the files you want to work on now. Other times you may just be using the filter as a quick way to find a file you want to open. In the latter case, you may want to clear the search when you open that file. This custom command offers that choice:
require("neo-tree").setup({
popup_border_style = "NC",
filesystem = {
window = {
mappings = {
["o"] = "open_and_clear_filter"
},
},
commands = {
open_and_clear_filter = function (state)
local node = state.tree:get_node()
if node and node.type == "file" then
local file_path = node:get_id()
-- reuse built-in commands to open and clear filter
local cmds = require("neo-tree.sources.filesystem.commands")
cmds.open(state)
cmds.clear_filter(state)
-- reveal the selected file without focusing the tree
require("neo-tree.sources.filesystem").navigate(state.path, file_path)
end
end,
}
}
})
If you want to take some custom action after a file has been renamed, you can override the built-in rename
command in your config and add your code to the callback provided for that action. The most obvious use for this would be to cleanup references to that file within your project.
require("neo-tree").setup({
filesystem = {
commands = {
rename = function(state)
local cc = require("neo-tree.sources.common.commands")
local fs = require("neo-tree.sources.filesystem")
cc.rename(state, function(original_path, new_path)
-- This is where you would do something like fix references to the file
-- with an LSP server.
-- <YOUR CODE HERE>
print("MY CUSTOM CODE: Renamed " .. original_path .. " to " .. new_path)
-- Don't forget to call fs.refresh() after you're done.
fs.refresh()
end)
end
},
})
Components are the blocks of text that get rendered for an item in the tree. Built-in components include things like "icon", "name", or "git_status". Adding a custom component involves two steps: defining a function to implement that component, and then using that component in a renderer.
This example adds the index number of a file that has been marked with Harpoon:
require("neo-tree").setup({
filesystem = {
components = {
harpoon_index = function(config, node, state)
local Marked = require("harpoon.mark")
local path = node:get_id()
local succuss, index = pcall(Marked.get_index_of, path)
if succuss and index and index > 0 then
return {
text = string.format(" ⥤ %d", index), -- <-- Add your favorite harpoon like arrow here
highlight = config.highlight or "NeoTreeDirectoryIcon",
}
else
return {}
end
end
},
renderers = {
file = {
{"icon"},
{"name", use_git_status_colors = true},
{"harpoon_index"}, --> This is what actually adds the component in where you want it
{"diagnostics"},
{"git_status", highlight = "NeoTreeDimText"},
}
}
},
})
This example uses the file_open
event to close the Neo-tree window when a file is opened. This applies to all windows and all sources at once.
require("neo-tree").setup({
event_handlers = {
{
event = "file_opened",
handler = function(file_path)
--auto close
require("neo-tree").close_all()
end
},
}
})