You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snacks.nvim文件资源管理器异常关闭及无法聚焦问题求助

Snacks.nvim 文件资源管理器问题修复

问题描述

  • 资源管理器已打开时,再次调用Snacks.explorer()会自动关闭
  • 调用Snacks.explorer()无法自动聚焦到资源管理器窗口

修复方案

1. 修改完整配置

替换原配置中的对应部分,以下是调整后的完整配置:

{
  "folke/snacks.nvim",
  opts = {
    bigfile = { enabled = true },
    dashboard = { enabled = true },
    explorer = { 
      enabled = true,
      auto_close = false,
      layout = { position = "left" },
      follow_file = true,
      tree = true,
    },
    indent = { enabled = true },
    input = { enabled = true },
    picker = {
      sources = {
        explorer = {
          layout = { layout = {position = "left"} },
          follow_file = true,
          tree = true,
          focus = "list",
          jump = { close = false },
          auto_close = false,
          win = {
              list = {
                keys = {
                  ["."] = "explorer_focus",
                },
              },
            },
        },
      },
    },
    notifier = { enabled = true },
    quickfile = { enabled = true },
    scope = { enabled = true },
    scroll = { enabled = true },
    statuscolumn = { enabled = true },
    words = { enabled = true },
  },
  keys = {
    { "<leader><space>", LazyVim.pick("files"), desc = "Smart Find Files" },
    { "<leader>,", function() Snacks.picker.buffers() end, desc = "Buffers" },
    { "<leader>:", function() Snacks.picker.command_history() end, desc = "Command History" },
    { "<leader>n", function() Snacks.picker.notifications() end, desc = "Notification History" },
    { "<leader>e", function()
        local explorer_win = Snacks.explorer.get_win()
        if explorer_win and vim.api.nvim_win_is_valid(explorer_win) then
          vim.api.nvim_set_current_win(explorer_win)
        else
          Snacks.explorer()
        end
      end,
      desc = "Focus/Open File Explorer"
    },
    { "<leader>gb", function() Snacks.picker.git_branches() end, desc = "Git Branches" },
    { "<leader>gl", function() Snacks.picker.git_log() end, desc = "Git Log" },
    { "<leader>gL", function() Snacks.picker.git_log_line() end, desc = "Git Log Line" },
    { "<leader>gs", function() Snacks.picker.git_status() end, desc = "Git Status" },
    { "<leader>gS", function() Snacks.picker.git_stash() end, desc = "Git Stash" },
    { "<leader>gd", function() Snacks.picker.git_diff() end, desc = "Git Diff (Hunks)" },
    { "<leader>gf", function() Snacks.picker.git_log_file() end, desc = "Git Log File" },
    { "gd", function() Snacks.picker.lsp_definitions() end, desc = "Goto Definition" },
    { "gD", function() Snacks.picker.lsp_declarations() end, desc = "Goto Declaration" },
    { "gr", function() Snacks.picker.lsp_references() end, nowait = true, desc = "References" },
    { "gI", function() Snacks.picker.lsp_implementations() end, desc = "Goto Implementation" },
    { "gy", function() Snacks.picker.lsp_type_definitions() end, desc = "Goto T[y]pe Definition" },
  },
}

2. 关键修改说明

  • 解决自动关闭问题:通过Snacks.explorer.get_win()判断窗口是否存在,仅在窗口不存在时调用打开方法,规避默认的toggle切换行为
  • 解决聚焦问题:直接调用vim.api.nvim_set_current_win()切换到已存在的资源管理器窗口,替代原配置中不可靠的模拟按键方式
  • 增强配置稳定性:在explorer根配置中明确设置auto_close = false,确保窗口不会被自动关闭

内容的提问来源于stack exchange,提问作者PlatinumBound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:19:53