[picker.explorer] How to use vim-tmux-navigator with picker #2931
|
In explorer (picker), I want to remove I set |
Replies: 5 comments 5 replies
|
There are a few gotchas:
-- lua/config/keymaps.lua
vim.keymap.set("n", "<c-h>", function() vim.notify("h") end, {})
vim.keymap.set("n", "<c-j>", function() vim.notify("j") end, {})
vim.keymap.set("n", "<c-k>", function() vim.notify("k") end, {})
vim.keymap.set("n", "<c-l>", function() vim.notify("l") end, {})-- lua/plugins/snacks.lua
---@module 'snacks'
return {
"folke/snacks.nvim",
---@type snacks.Config
opts = {
picker = {
win = {
input = {
keys = {
["<c-j>"] = false,
["<c-k>"] = false,
},
},
list = {
keys = {
["<c-j>"] = false,
["<c-k>"] = false,
},
},
},
},
},
}Considering p.2, it may be easier to create built-in actions and override the keymaps directly in the plugin config: -- lua/plugins/snacks.lua
---@module 'snacks'
return {
"folke/snacks.nvim",
---@type snacks.Config
opts = {
picker = {
actions = {
tmux_h = function() end,
tmux_j = function() end,
tmux_k = function() end,
tmux_l = function() end,
},
win = {
input = {
keys = {
["<c-h>"] = { "tmux_h", mode = { "i", "n" } },
["<c-j>"] = { "tmux_j", mode = { "i", "n" } },
["<c-k>"] = { "tmux_k", mode = { "i", "n" } },
["<c-l>"] = { "tmux_l", mode = { "i", "n" } },
},
},
list = {
keys = {
["<c-h>"] = "tmux_h",
["<c-j>"] = "tmux_j",
["<c-k>"] = "tmux_k",
["<c-l>"] = "tmux_l",
},
},
},
},
},
} |
|
Thank you. First, I am not using I tried the custom function like you suggested and I have this local function tmux_nav(direction)
local map = {
h = "TmuxNavigateLeft",
j = "TmuxNavigateDown",
k = "TmuxNavigateUp",
l = "TmuxNavigateRight",
}
print("testing")
if Utils.fn.is_in_tmux() and Pack.has("vim-tmux-navigator") then
print("work")
vim.cmd(map[direction])
else
vim.cmd("wincmd " .. direction)
end
endThen I have this actions = {
tmux_h = function()
tmux_nav("h")
end,
tmux_j = function()
tmux_nav("j")
end,
tmux_k = function()
tmux_nav("k")
end,
tmux_l = function()
tmux_nav("l")
end,
},
win = {
list = {
keys = {
["<c-c>"] = "",
["<c-h>"] = "tmux_h",
["<c-j>"] = "tmux_j",
["<c-k>"] = "tmux_k",
["<c-l>"] = "tmux_l",
["s"] = "edit_vsplit",
["S"] = "edit_split",
},
},
},The print message I added was shown when I hit the keymap but it does not perform the required action. |
|
setting expr to true for some reason makes it work but it throws an error:
|
|
It's expected behavior for For the fix, we simply call the command in the Via disable---@module 'snacks'
vim.g.tmux_navigator_no_mappings = 1
vim.pack.add({
{ src = "https://github.com/christoomey/vim-tmux-navigator" },
{ src = "https://github.com/folke/snacks.nvim" },
})
vim.keymap.set("n", "<Space>e", function()
Snacks.picker.explorer()
end, {})
local tmux_rhs = function(_, cmd)
local run_cmd = function()
vim.cmd(cmd)
end
return function()
---@type snacks.Picker
local picker = vim.iter(Snacks.picker.get()):find(function(picker)
return picker:is_focused()
end)
if picker then
local root_win = assert(picker.layout.root.win)
local next_win = vim.api.nvim_win_call(root_win, function()
run_cmd()
return vim.api.nvim_get_current_win()
end)
vim.api.nvim_set_current_win(next_win)
else
run_cmd()
end
end
end
vim.keymap.set({ "i", "n" }, "<c-h>", tmux_rhs("h", "TmuxNavigateLeft"), {})
vim.keymap.set({ "i", "n" }, "<c-j>", tmux_rhs("j", "TmuxNavigateDown"), {})
vim.keymap.set({ "i", "n" }, "<c-k>", tmux_rhs("k", "TmuxNavigateUp"), {})
vim.keymap.set({ "i", "n" }, "<c-l>", tmux_rhs("l", "TmuxNavigateRight"), {})
require("snacks").setup({
picker = {
win = {
input = {
keys = {
["<c-j>"] = false,
["<c-k>"] = false,
},
},
list = {
keys = {
["<c-j>"] = false,
["<c-k>"] = false,
},
},
},
},
})Via override---@module 'snacks'
-- NOTE: Keep the plugin's default keymaps
-- vim.g.tmux_navigator_no_mappings = 1
vim.pack.add({
{ src = "https://github.com/christoomey/vim-tmux-navigator" },
{ src = "https://github.com/folke/snacks.nvim" },
})
vim.keymap.set("n", "<Space>e", function()
Snacks.picker.explorer()
end, {})
local make_tmux_action = function(cmd)
return function(picker) ---@param picker snacks.Picker
local root_win = assert(picker.layout.root.win)
local next_win = vim.api.nvim_win_call(root_win, function()
vim.cmd(cmd)
return vim.api.nvim_get_current_win()
end)
vim.api.nvim_set_current_win(next_win)
end
end
require("snacks").setup({
picker = {
actions = {
tmux_h = make_tmux_action("TmuxNavigateLeft"),
tmux_j = make_tmux_action("TmuxNavigateDown"),
tmux_k = make_tmux_action("TmuxNavigateUp"),
tmux_l = make_tmux_action("TmuxNavigateRight"),
},
win = {
input = {
keys = {
["<c-h>"] = { "tmux_h", mode = { "i", "n" } },
["<c-j>"] = { "tmux_j", mode = { "i", "n" } },
["<c-k>"] = { "tmux_k", mode = { "i", "n" } },
["<c-l>"] = { "tmux_l", mode = { "i", "n" } },
},
},
list = {
keys = {
["<c-h>"] = "tmux_h",
["<c-j>"] = "tmux_j",
["<c-k>"] = "tmux_k",
["<c-l>"] = "tmux_l",
},
},
},
},
}) |
It's expected behavior for
vim-tmux-navigatorto jump from a floating window to the previous window. Sincesnacks.nvimuses multiple floating windows, you're moving to thepicker.mainwindow in the same way. Ifauto_closeis enabled, the picker closes when it loses focus.For the fix, we simply call the command in the
rootwindow context, which is a non-floating window in the case of the explorer.Via disable