ITADN
mikavilpas/yazi.nvim
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

🎲 一个 Neovim 插件,用于 yazi

shield image for plugin usage

LuaRocks Type checked codebase

Yazi 是一款极速的终端文件管理器。此插件允许你在 Neovim 中以浮动窗口打开 yazi。

https://github.com/mikavilpas/yazi.nvim/assets/300791/c7ff98ee-54d6-4ad0-9318-903e4b674f84

✨ 功能

  • <f1> 显示所有键位映射!
  • 在浮动窗口中打开 yazi。所有可见的分屏都会作为 yazi 标签页打开,以便轻松快速地导航。
  • 在 yazi 中悬停的文件会在 Neovim 中高亮显示,以直观地展示你相对于 Neovim 会话的位置。目前这适用于你已打开的分屏。
  • 在 yazi 中选择的文件可以通过多种方式打开:作为当前缓冲区、垂直分屏、水平分屏、 新标签页、作为 quickfix 项……
    • 如果你在可视模式下选择了文件路径,yazi 将打开该文件而不是当前文件
  • 与其他插件和工具的集成(如果已安装):
    • 对于 telescope.nvim, fzf-lua.nvimsnacks.picker:你可以在 yazi 所在的目录中执行 grep/搜索。选择一些文件可将搜索范围限制在这些文件中。
    • 使用捆绑的 snacks.bufdelete 版本, 让 yazi 关闭缓冲区并保持窗口布局。当在 yazi 中删除已打开的文件时,会发生这种情况。
    • 对于 grug-far.nvim:你可以在 yazi 所在的目录中 执行搜索和替换
    • 复制从起始文件到当前悬停文件的相对路径。在 linux 和 windows 上需要 realpath (1),或在 osx 上需要 grealpath
  • 如果选择了多个文件,可以将它们发送到 quickfix 列表
  • (可选)为目录打开 yazi 而不是 netrw
  • 在 yazi 中被重命名、移动或删除的文件会与 Neovim 中打开的缓冲区保持同步
  • 这些文件也会与当前正在运行的 LSP 服务器保持同步。技术说明见 documentation/for-developers/lsp-renaming.md
  • 可自定义的键位绑定
  • Yazi 插件和风味(flavors)的插件管理(documentation
  • 🆕 向 yazi 发送自定义命令(documentation

要在 yazi 中预览图像,请参阅 Yazi 的 与 Neovim 相关的文档

📦 安装

首先,请确保你已满足以下要求:

  • Neovim stable 或 nightly
  • 较新版本的 yazi
  • 如果在 Windows 上,最低支持版本为 Windows 11

[!TIP]

你可以运行 :checkhealth yazi 来检查是否已安装并正常工作兼容的版本。

⚙️ 配置

使用 lazy.nvim

这是推荐的安装方式。

请注意,当使用 lazy.nvim 等插件管理器时,yazi.nvim 会自动为你添加并懒加载一些最小依赖项。 要查看安装了哪些依赖项,请参阅 lazy.lua。如果你没有使用 lazy.nvim(或 rocks.nvim),则需要自行安装依赖项。另请参阅 issue 306 中的讨论以及 其他使用此功能的 Neovim 插件示例

---@type LazySpec
return {
  "mikavilpas/yazi.nvim",
  version = "*", -- use the latest stable version
  event = "VeryLazy",
  dependencies = {
    { "nvim-lua/plenary.nvim", lazy = true },
  },
  keys = {
    -- 👇 in this section, choose your own keymappings!
    {
      "<leader>-",
      mode = { "n", "v" },
      "<cmd>Yazi<cr>",
      desc = "Open yazi at the current file",
    },
    {
      -- Open in the current working directory
      "<leader>cw",
      "<cmd>Yazi cwd<cr>",
      desc = "Open the file manager in nvim's working directory",
    },
    {
      "<c-up>",
      "<cmd>Yazi toggle<cr>",
      desc = "Resume the last yazi session",
    },
  },
  ---@type YaziConfig | {}
  opts = {
    -- if you want to open yazi instead of netrw, see below for more info
    open_for_directories = false,
    keymaps = {
      show_help = "<f1>",
    },
  },
  -- 👇 if you use `open_for_directories=true`, this is recommended
  init = function()
    -- mark netrw as loaded so it's not loaded at all.
    --
    -- More details: https://github.com/mikavilpas/yazi.nvim/issues/802
    vim.g.loaded_netrwPlugin = 1
  end,
}

不使用包管理器

说明

如果你没有使用 lazy.nvim,请参阅 lazy.lua 了解所需的依赖项。

-- (Obtain yazi.nvim and its dependencies using your preferred method first)
--
-- Next, map a key to open yazi.nvim
vim.keymap.set("n", "<leader>-", function()
  require("yazi").yazi()
end)

-- 👇 if you use `open_for_directories=true`, this is recommended.
--
-- mark netrw as loaded so it's not loaded at all.
-- More details: https://github.com/mikavilpas/yazi.nvim/issues/802
vim.g.loaded_netrwPlugin = 1
vim.api.nvim_create_autocmd("UIEnter", {
  callback = function()
    require("yazi").setup({
      open_for_directories = true,
    })
  end,
})

⌨️ 键位绑定

以下是 yazi 打开时可用的默认键位绑定:

  • <f1>: 显示帮助菜单
  • <c-v>: 在垂直分屏中打开选中的文件
  • <c-x>: 在水平分屏中打开选中的文件
  • <c-t>: 在新标签页中打开选中的文件
  • <c-q>: 将选中的文件发送到 quickfix 列表
  • 此外还有与其他插件的集成,您需要单独安装:
    • <c-s>: 如果可用,使用 telescopelive_grep 在当前 yazi 目录中搜索。可选地使用 fzf-lua.nvimsnacks.picker 或提供您自己的实现 - 请参阅配置部分的说明以获取更多信息。
      • 如果在 yazi 中选中了多个文件/目录,搜索和替换将仅在选中的 文件/目录中进行
    • <c-g>: 如果可用,使用 grug-far 在当前 yazi 目录中搜索和替换
      • 如果在 yazi 中选中了多个文件/目录,操作仅限于这些文件/目录
    • <c-y>: 将选中文件的相对路径复制到剪贴板。在 OSX 上需要 GNU realpathgrealpath
    • <tab>: 使 yazi 跳转到 Neovim 中打开的缓冲区。请参阅 #232 以获取更多信息

⚙️⚙️ 高级配置

[!IMPORTANT]

你无需设置这些选项中的任何一个。默认值对大多数用户来说已经足够。

对于高级配置,建议配置好你的 Lua 语言服务器,以便对配置进行类型检查并避免错误。

关于如何操作的帮助,文档中有一个关于 Neovim 开发工具的章节,见 documentation

你可以通过设置以下任意选项来可选地配置 yazi.nvim。

return {
  -- ... other lazy.nvim configuration from above

  ---@type YaziConfig | {}
  opts = {
    -- Below is the default configuration. It is optional to set these values.
    -- You can customize the configuration for each yazi call by passing it to
    -- yazi() explicitly

    -- enable this if you want to open yazi instead of netrw.
    -- Note that if you enable this, you need to call yazi.setup() to
    -- initialize the plugin. lazy.nvim does this for you in certain cases.
    --
    -- If you are also using neotree, you may prefer not to bring it up when
    -- opening a directory:
    -- {
    --   "nvim-neo-tree/neo-tree.nvim",
    --   opts = {
    --     filesystem = {
    --       hijack_netrw_behavior = "disabled",
    --     },
    --   },
    -- }
    open_for_directories = false,

    -- open visible splits and quickfix items as yazi tabs for easy navigation
    -- https://github.com/mikavilpas/yazi.nvim/pull/359
    open_multiple_tabs = false,

    -- when yazi is closed with no file chosen, change the Neovim working
    -- directory to the directory that yazi was in before it was closed. Defaults
    -- to being off (`false`)
    change_neovim_cwd_on_close = false,

    highlight_groups = {
      -- See https://github.com/mikavilpas/yazi.nvim/pull/180
      hovered_buffer = nil,
      -- See https://github.com/mikavilpas/yazi.nvim/pull/351
      hovered_buffer_in_same_directory = nil,
    },

    -- the floating window scaling factor. 1 means 100%, 0.9 means 90%, etc.
    floating_window_scaling_factor = 0.9,

    -- the transparency of the yazi floating window (0-100). See :h winblend
    yazi_floating_window_winblend = 0,

    -- the type of border to use for the floating window. Can be many values,
    -- including 'none', 'rounded', 'single', 'double', 'shadow', etc. For
    -- more information, see :h nvim_open_win
    yazi_floating_window_border = "rounded",

    -- the zindex of the yazi floating window. Can be used to make the yazi
    -- window fullscreen. See `:h nvim_open_win()` for more information.
    yazi_floating_window_zindex = nil,

    -- the log level to use. Off by default, but can be used to diagnose
    -- issues. You can find the location of the log file by running
    -- `:checkhealth yazi` in Neovim. Also check out the "reproducing issues"
    -- section below
    log_level = vim.log.levels.OFF,

    -- what Neovim should do a when a file was opened (selected) in yazi.
    -- Defaults to simply opening the file.
    open_file_function = function(chosen_file, config, state) end,

    -- customize the keymaps that are active when yazi is open and focused. The
    -- defaults are listed below. Note that the keymaps simply hijack input and
    -- they are never sent to yazi, so only try to map keys that are never
    -- needed by yazi.
    --
    -- Also:
    -- - use e.g. `open_file_in_tab = false` to disable a keymap
    -- - you can customize only some of the keymaps (not all of them)
    -- - you can opt out of all keymaps by setting `keymaps = false`
    keymaps = {
      show_help = "<f1>",
      open_file_in_vertical_split = "<c-v>",
      open_file_in_horizontal_split = "<c-x>",
      open_file_in_tab = "<c-t>",
      grep_in_directory = "<c-s>",
      replace_in_directory = "<c-g>",
      cycle_open_buffers = "<tab>",
      copy_relative_path_to_selected_files = "<c-y>",
      send_to_quickfix_list = "<c-q>",
      change_working_directory = "<c-\\>",
      open_and_pick_window = "<c-o>",
    },

    -- completely override the keymappings for yazi. This function will be
    -- called in the context of the yazi terminal buffer.
    set_keymappings_function = function(yazi_buffer_id, config, context) end,

    -- some yazi.nvim commands copy text to the clipboard. This is the register
    -- yazi.nvim should use for copying. Defaults to "*", the system clipboard
    clipboard_register = "*",

    hooks = {
      -- if you want to execute a custom action when yazi has been opened,
      -- you can define it here.
      yazi_opened = function(preselected_path, yazi_buffer_id, config)
        -- you can optionally modify the config for this specific yazi
        -- invocation if you want to customize the behaviour
      end,

      -- when yazi was successfully closed
      yazi_closed_successfully = function(chosen_file, config, state) end,

      -- when yazi opened multiple files. The default is to send them to the
      -- quickfix list, but if you want to change that, you can define it here
      yazi_opened_multiple_files = function(chosen_files, config, state) end,

      -- This function is called when yazi is ready to process events.
      on_yazi_ready = function(buffer, config, process_api) end,

      before_opening_window = function(window_options) end,
    },

    -- highlight buffers in the same directory as the hovered buffer
    highlight_hovered_buffers_in_same_directory = true,

    integrations = {
      --- What should be done when the user wants to grep in a directory
      grep_in_directory = function(directory)
        -- the default implementation uses telescope if available, otherwise nothing
      end,

      grep_in_selected_files = function(selected_files)
        -- similar to grep_in_directory, but for selected files
      end,

      --- Similarly, search and replace in the files in the directory
      replace_in_directory = function(directory)
        -- default: grug-far.nvim
      end,

      replace_in_selected_files = function(selected_files)
        -- default: grug-far.nvim
      end,

      -- `grealpath` on OSX, (GNU) `realpath` otherwise
      resolve_relative_path_application = "",

      -- the way to resolve relative paths. The default_implementation can be
      -- customized with a function. See
      -- documentation/copy-relative-path-to-files.md for more information.
      resolve_relative_path_implementation = function(args, get_relative_path)
      end,

      -- how to delete (close) a buffer. Defaults to a bundled version of
      -- `snacks.bufdelete`, copied from https://github.com/folke/snacks.nvim,
      -- which maintains the window layout. See the `types.lua` file for more
      -- information for the available options.
      bufdelete_implementation = "bundled-snacks",

      -- add an action to a file picker to copy the relative path to the
      -- selected file(s). The implementation is the same as for the
      -- `copy_relative_path_to_selected_files` yazi.nvim keymap. Currently
      -- only snacks.nvim is supported. Documentation can be found in the
      -- keybindings section of the readme.
      --
      -- available options:
      -- - nil (default, no action added)
      -- - "snacks.picker" (snacks.nvim)
      picker_add_copy_relative_path_action = nil,
    },

    future_features = {
      -- use a file to store the last directory that yazi was in before it was
      -- closed. Defaults to `true`.
      use_cwd_file = true,
    },
  },
}

[!TIP]

如果你正在为特殊用例添加自定义绑定,可以使用 lua api。它允许按调用粒度自定义 配置。例如,你可以通过调用 require('yazi').yazi({open_for_directories = true}) 来覆盖此特定 调用的一些默认设置,从而以不同的配置打开 yazi。

🪛 自定义 yazi

Yazi 具有高度可定制性。它拥有自己的插件和事件系统、主题以及按键绑定。本节列出了 我喜爱的一些插件和主题。

贡献

请参阅 COMMUNITY.md 以获取有关该项目的更多信息!

如果遇到问题

参见 reproducing-issues.md

Powershell 用户:请参阅 issue 675 以获取 Powershell 特定 配置。