diff --git a/README.md b/README.md
index 8e79d494..e28e43ec 100644
--- a/README.md
+++ b/README.md
@@ -304,73 +304,91 @@ Create nvim-tree hotkey `z` that will create/open named instance `tree` of grug-
Nvim tree lazy plugin setup
+Small video of it in action: https://github.com/MagicDuck/grug-far.nvim/issues/165#issuecomment-2257439367
+
```lua
return {
"nvim-tree/nvim-tree.lua",
dependencies = "nvim-tree/nvim-web-devicons",
config = function()
- local nvimtree = require("nvim-tree")
-
- -- custom on attach function to remove some default mappings and add custom ones
- local function my_on_attach(bufnr)
- local api = require("nvim-tree.api")
- local lib = require("nvim-tree.lib")
-
- local function opts(desc)
- return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
- end
-
- -- defaults
- api.config.mappings.default_on_attach(bufnr)
-
- -- add custom key mapping to search in directory with grug-far
- vim.keymap.set("n", "z", function()
- local node = lib.get_node_at_cursor()
- local grugFar = require("grug-far")
- if node then
- -- get directory of current file if it's a file
- local path
- if node.type == "directory" then
- -- Keep the full path for directories
- path = node.absolute_path
- else
- -- Get the directory of the file
- path = vim.fn.fnamemodify(node.absolute_path, ":h")
- end
-
- -- escape all spaces in the path with "\ "
- path = path:gsub(" ", "\\ ")
-
+ -- https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree.lua#L342
+ require("nvim-tree").setup {
+ commands = {
+ -- create a new neo-tree command
+ grug_far_replace = function(state)
+ local node = state.tree:get_node()
local prefills = {
- paths = path,
+ -- get the current path and get the parent directory if a file is selected
+ paths = node.type == "directory" and node:get_id() or vim.fn.fnamemodify(node:get_id(), ":h"),
}
+ local grug_far = require "grug-far"
-- instance check
- if not grugFar.has_instance("tree") then
- grugFar.open({
- instanceName = "tree",
+ if not grug_far.has_instance "explorer" then
+ grug_far.open {
+ instanceName = "explorer",
prefills = prefills,
- staticTitle = "Find and Replace from Tree",
- })
+ staticTitle = "Find and Replace from Explorer",
+ }
else
- grugFar.open_instance("tree")
+ grug_far.open_instance "explorer"
-- updating the prefills without clearing the search and other fields
- grugFar.update_instance_prefills("tree", prefills, false)
+ grug_far.update_instance_prefills("explorer", prefills, false)
end
- end
- end, opts("Search in directory"))
- end
+ end,
+ },
+ window = {
+ mappings = {
+ -- map our new command to z
+ z = "grug_far_replace",
+ },
+ },
+ -- rest of your config
+ }
+ end,
+}
+```
+
- -- https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree.lua#L342
- nvimtree.setup({
- on_attach = my_on_attach,
+
+Oil explorer lazy plugin setup
+
+```lua
+return {
+ "stevearc/oil.nvim",
+ config = function()
+ local oil = require "oil"
+ oil.setup {
+ keymaps = {
+ -- create a new mapping, gs, to search and replace in the current directory
+ gs = {
+ callback = function()
+ -- get the current directory
+ local prefills = { paths = oil.get_current_dir() }
+
+ local grug_far = require "grug-far"
+ -- instance check
+ if not grug_far.has_instance "explorer" then
+ grug_far.open {
+ instanceName = "explorer",
+ prefills = prefills,
+ staticTitle = "Find and Replace from Explorer",
+ }
+ else
+ grug_far.open_instance "explorer"
+ -- updating the prefills without clearing the search and other fields
+ grug_far.update_instance_prefills("explorer", prefills, false)
+ end
+ end,
+ desc = "oil: Search in directory",
+ },
+ },
-- rest of your config
- })
+ }
end,
}
```
-Small video of it in action: https://github.com/MagicDuck/grug-far.nvim/issues/165#issuecomment-2257439367
## ❓ Q&A