[mini.files] Highlight target window #2177
Replies: 2 comments
-
|
This is an interesting idea! Thanks for sharing! I'd go with a bit more compact solution: local hl_target_win = function()
-- Only highlight a window if there is more than one possible target
local n_win = 0
for _, win_id in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
-- Treat only normal windows (not floating) as a target
local is_target = vim.api.nvim_win_get_config(win_id).relative == ''
n_win = n_win + (is_target and 1 or 0)
end
if n_win == 1 then return end
-- Temporarily set 'winhighlight' and setup restore after closing explorer
local target_win_id = MiniFiles.get_explorer_state().target_window
local winhl_orig = vim.wo[target_win_id].winhighlight
vim.wo[target_win_id].winhighlight = 'Normal:Visual,SignColumn:Visual'
local restore = function() vim.wo[target_win_id].winhighlight = winhl_orig end
vim.api.nvim_create_autocmd('User', { once = true, pattern = 'MiniFilesExplorerClose', callback = restore })
end
local au_opts = { pattern = 'MiniFilesExplorerOpen', callback = hl_target_win, desc = 'Highlight target window' }
vim.api.nvim_create_autocmd('User', au_opts)Also note that this will not work well with documented example of modifying target window. Edit: do not use |
Beta Was this translation helpful? Give feedback.
-
|
My original post did not quite work as intended due to the way window local options behave. The previous implementation used the
Consequently, when the user opened a file in the explorer, our restore function to remove the window local option was applied to the correct window, but it already contained the new file. So when the user switched back to the prior buffer in the same window, it remembered the window highlight options that were previously set and incorrectly highlighted the window. This new implementation uses highlight namespaces to achieve the originally intended effect. A window can be assigned a highlight namespace, which can define hl groups that take priority over the global hl groups. Like the prior implementation, I save a copy of any previously applied highlight namespace so it can be restored upon exit of the explorer. This version works as expected because the hl namespace is applied to the window regardless of what buffer is contained within. -- Create hl namespace to highlight 'mini.files' target window
local highlight_ns = vim.api.nvim_create_namespace("highlight_minifiles_target")
vim.api.nvim_set_hl(highlight_ns, "Normal", { link = "Visual" })
vim.api.nvim_set_hl(highlight_ns, "SignColumn", { link = "Visual" })
local hl_target_win = function()
-- Only highlight a window if there is more than one possible target
local possible_targets = vim
.iter(vim.api.nvim_tabpage_list_wins(0))
:filter(function(w) return vim.api.nvim_win_get_config(w).relative == "" end)
:fold(0, function(acc, _) return acc + 1 end)
if possible_targets == 1 then return end
-- Temporarily set a window hl namespace and setup restore after closing explorer
local target_win_id = MiniFiles.get_explorer_state().target_window
local orig_hl_ns = vim.api.nvim_get_hl_ns({ winid = target_win_id })
local restore = function() vim.api.nvim_win_set_hl_ns(target_win_id, orig_hl_ns ~= -1 and orig_hl_ns or 0) end
vim.api.nvim_win_set_hl_ns(target_win_id, highlight_ns)
vim.api.nvim_create_autocmd("User", { once = true, pattern = "MiniFilesExplorerClose", callback = restore })
end
local au_opts = { pattern = "MiniFilesExplorerOpen", callback = hl_target_win, desc = "Highlight target window" }
vim.api.nvim_create_autocmd("User", au_opts) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Update: see #2177 (comment) for the correct implementation.
Have you ever forgotten which window you were in when you opened 'mini.files' and been unsure where your selected file will appear? If so, you might like this auto command. When there’s at least one split on-screen, it highlights the target window while you’re in the 'mini.files' explorer.
Screen.Recording.2025-12-16.at.12.17.36.PM.mov
Beta Was this translation helpful? Give feedback.
All reactions