Skip to content
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

File explorer recipe improvements #256

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 66 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,73 +304,91 @@ Create nvim-tree hotkey `z` that will create/open named instance `tree` of grug-
<details>
<summary>Nvim tree lazy plugin setup</summary>

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,
}
```
</details>

-- https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree.lua#L342
nvimtree.setup({
on_attach = my_on_attach,
<details>
<summary>Oil explorer lazy plugin setup</summary>

```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,
}
```
</details>
Small video of it in action: https://github.com/MagicDuck/grug-far.nvim/issues/165#issuecomment-2257439367

## ❓ Q&A

Expand Down
Loading