|
| 1 | +return { |
| 2 | + "echasnovski/mini.files", |
| 3 | + version = false, |
| 4 | + config = function() |
| 5 | + require("mini.files").setup({ |
| 6 | + -- Customization of shown content |
| 7 | + content = { |
| 8 | + -- Predicate for which file system entries to show |
| 9 | + filter = nil, |
| 10 | + -- What prefix to show to the left of file system entry |
| 11 | + prefix = nil, |
| 12 | + -- In which order to show file system entries |
| 13 | + sort = nil, |
| 14 | + }, |
| 15 | + |
| 16 | + -- Module mappings created only inside explorer. |
| 17 | + -- Use `''` (empty string) to not create one. |
| 18 | + mappings = { |
| 19 | + close = "q", |
| 20 | + go_in = "l", |
| 21 | + go_in_plus = "L", |
| 22 | + go_out = "h", |
| 23 | + go_out_plus = "H", |
| 24 | + reset = "<BS>", |
| 25 | + reveal_cwd = "@", |
| 26 | + show_help = "g?", |
| 27 | + synchronize = "=", |
| 28 | + trim_left = "<", |
| 29 | + trim_right = ">", |
| 30 | + }, |
| 31 | + |
| 32 | + -- General options |
| 33 | + options = { |
| 34 | + -- Whether to delete permanently or move into module-specific trash |
| 35 | + permanent_delete = true, |
| 36 | + -- Whether to use for editing directories |
| 37 | + use_as_default_explorer = true, |
| 38 | + }, |
| 39 | + |
| 40 | + -- Customization of explorer windows |
| 41 | + windows = { |
| 42 | + -- Maximum number of windows to show side by side |
| 43 | + max_number = math.huge, |
| 44 | + -- Whether to show preview of file/directory under cursor |
| 45 | + preview = false, |
| 46 | + -- Width of focused window |
| 47 | + width_focus = 50, |
| 48 | + -- Width of non-focused window |
| 49 | + width_nofocus = 15, |
| 50 | + -- Width of preview window |
| 51 | + width_preview = 25, |
| 52 | + }, |
| 53 | + }) |
| 54 | + |
| 55 | + -- Global keybindings for mini.files |
| 56 | + vim.keymap.set("n", "<leader>e", function() |
| 57 | + require("mini.files").open() |
| 58 | + end, { desc = "Open mini.files (current dir)" }) |
| 59 | + |
| 60 | + vim.keymap.set("n", "<leader>E", function() |
| 61 | + require("mini.files").open(vim.api.nvim_buf_get_name(0)) |
| 62 | + end, { desc = "Open mini.files (current file)" }) |
| 63 | + |
| 64 | + vim.keymap.set("n", "-", function() |
| 65 | + require("mini.files").open() |
| 66 | + end, { desc = "Open parent directory" }) |
| 67 | + |
| 68 | + -- Mini.files specific keybindings and autocommands |
| 69 | + vim.api.nvim_create_autocmd("User", { |
| 70 | + pattern = "MiniFilesBufferCreate", |
| 71 | + callback = function(args) |
| 72 | + local buf_id = args.data.buf_id |
| 73 | + |
| 74 | + -- Add buffer-local keybindings |
| 75 | + vim.keymap.set("n", "<C-s>", function() |
| 76 | + -- Split window and open file |
| 77 | + local cur_target = require("mini.files").get_fs_entry() |
| 78 | + if cur_target and cur_target.fs_type == "file" then |
| 79 | + require("mini.files").close() |
| 80 | + vim.cmd("split " .. cur_target.path) |
| 81 | + end |
| 82 | + end, { buffer = buf_id, desc = "Split and open file" }) |
| 83 | + |
| 84 | + vim.keymap.set("n", "<C-v>", function() |
| 85 | + -- Vertical split and open file |
| 86 | + local cur_target = require("mini.files").get_fs_entry() |
| 87 | + if cur_target and cur_target.fs_type == "file" then |
| 88 | + require("mini.files").close() |
| 89 | + vim.cmd("vsplit " .. cur_target.path) |
| 90 | + end |
| 91 | + end, { buffer = buf_id, desc = "Vertical split and open file" }) |
| 92 | + |
| 93 | + vim.keymap.set("n", "<C-t>", function() |
| 94 | + -- Open in new tab |
| 95 | + local cur_target = require("mini.files").get_fs_entry() |
| 96 | + if cur_target and cur_target.fs_type == "file" then |
| 97 | + require("mini.files").close() |
| 98 | + vim.cmd("tabnew " .. cur_target.path) |
| 99 | + end |
| 100 | + end, { buffer = buf_id, desc = "Open in new tab" }) |
| 101 | + |
| 102 | + -- Create new file/directory |
| 103 | + vim.keymap.set("n", "a", function() |
| 104 | + local cur_target = require("mini.files").get_fs_entry() |
| 105 | + local path = cur_target and cur_target.path or require("mini.files").get_explorer_state().cwd |
| 106 | + local new_name = vim.fn.input("Create: " .. path .. "/") |
| 107 | + if new_name and new_name ~= "" then |
| 108 | + if new_name:sub(-1) == "/" then |
| 109 | + -- Create directory |
| 110 | + vim.fn.mkdir(path .. "/" .. new_name, "p") |
| 111 | + else |
| 112 | + -- Create file |
| 113 | + local new_file = io.open(path .. "/" .. new_name, "w") |
| 114 | + if new_file then |
| 115 | + new_file:close() |
| 116 | + end |
| 117 | + end |
| 118 | + require("mini.files").refresh() |
| 119 | + end |
| 120 | + end, { buffer = buf_id, desc = "Create new file/directory" }) |
| 121 | + |
| 122 | + -- Rename file/directory |
| 123 | + vim.keymap.set("n", "r", function() |
| 124 | + local cur_target = require("mini.files").get_fs_entry() |
| 125 | + if cur_target then |
| 126 | + local old_name = vim.fn.fnamemodify(cur_target.path, ":t") |
| 127 | + local new_name = vim.fn.input("Rename to: ", old_name) |
| 128 | + if new_name and new_name ~= "" and new_name ~= old_name then |
| 129 | + local new_path = vim.fn.fnamemodify(cur_target.path, ":h") .. "/" .. new_name |
| 130 | + os.rename(cur_target.path, new_path) |
| 131 | + require("mini.files").refresh() |
| 132 | + end |
| 133 | + end |
| 134 | + end, { buffer = buf_id, desc = "Rename file/directory" }) |
| 135 | + |
| 136 | + -- Delete file/directory |
| 137 | + vim.keymap.set("n", "d", function() |
| 138 | + local cur_target = require("mini.files").get_fs_entry() |
| 139 | + if cur_target then |
| 140 | + local confirm = vim.fn.confirm("Delete " .. cur_target.path .. "?", "&Yes\n&No", 2) |
| 141 | + if confirm == 1 then |
| 142 | + if cur_target.fs_type == "directory" then |
| 143 | + vim.fn.delete(cur_target.path, "rf") |
| 144 | + else |
| 145 | + vim.fn.delete(cur_target.path) |
| 146 | + end |
| 147 | + require("mini.files").refresh() |
| 148 | + end |
| 149 | + end |
| 150 | + end, { buffer = buf_id, desc = "Delete file/directory" }) |
| 151 | + end, |
| 152 | + }) |
| 153 | + |
| 154 | + -- Auto-close mini.files when it's the last window |
| 155 | + vim.api.nvim_create_autocmd("User", { |
| 156 | + pattern = "MiniFilesBufferUpdate", |
| 157 | + callback = function() |
| 158 | + if vim.bo.filetype == "minifiles" then |
| 159 | + -- Check if this is the only window left |
| 160 | + local windows = vim.api.nvim_list_wins() |
| 161 | + local minifiles_windows = 0 |
| 162 | + for _, win in ipairs(windows) do |
| 163 | + local buf = vim.api.nvim_win_get_buf(win) |
| 164 | + if vim.api.nvim_buf_get_option(buf, "filetype") == "minifiles" then |
| 165 | + minifiles_windows = minifiles_windows + 1 |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + if #windows == minifiles_windows then |
| 170 | + vim.cmd("quit") |
| 171 | + end |
| 172 | + end |
| 173 | + end, |
| 174 | + }) |
| 175 | + end, |
| 176 | +} |
0 commit comments