From 9d90893c377b6b75230e4bad177f8d0103ceafe4 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 20 Aug 2023 00:40:24 +0000 Subject: [PATCH] refactor!: disable netrw by default (#155) If you use oil and you want to still use netrw, set `default_file_explorer = false`. It is nonsensical to both use netrw _and_ have oil hijack directory buffers (which was the case for the default config). It also causes undefined behavior and bugs. When `default_file_explorer = true` (the default) oil will now disable netrw for you. --- .github/generate.py | 5 +++-- README.md | 16 ++++------------ doc/oil.txt | 5 +++-- lua/oil/config.lua | 5 +++-- lua/oil/init.lua | 27 +++++++-------------------- 5 files changed, 20 insertions(+), 38 deletions(-) diff --git a/.github/generate.py b/.github/generate.py index 87881c5..1508168 100755 --- a/.github/generate.py +++ b/.github/generate.py @@ -73,10 +73,11 @@ def update_readme_toc(): def update_config_options(): config_file = os.path.join(ROOT, "lua", "oil", "config.lua") - opt_lines = read_section(config_file, r"^\s*local default_config =", r"^}$") + opt_lines = ['\n```lua\nrequire("oil").setup({\n'] + opt_lines.extend(read_section(config_file, r"^\s*local default_config =", r"^}$")) replace_section( README, - r"^require\(\"oil\"\)\.setup\(\{$", + r"^## Options$", r"^}\)$", opt_lines, ) diff --git a/README.md b/README.md index 8a394c2..a1c72ed 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ Then open a directory with `nvim .`. Use `` to open a file/directory, and `- If you want to mimic the `vim-vinegar` method of navigating to the parent directory of a file, add this keymap: ```lua -vim.keymap.set("n", "-", require("oil").open, { desc = "Open parent directory" }) +vim.keymap.set("n", "-", "Oil", { desc = "Open parent directory" }) ``` You can open a directory with `:edit ` or `:Oil `. To open oil in a floating window, do `:Oil --float `. @@ -123,6 +123,9 @@ You can open a directory with `:edit ` or `:Oil `. To open oil in a ```lua require("oil").setup({ + -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) + -- Set to false if you still want to use netrw. + default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns columns = { @@ -147,8 +150,6 @@ require("oil").setup({ conceallevel = 3, concealcursor = "n", }, - -- Oil will take over directory buffers (e.g. `vim .` or `:e src/` - default_file_explorer = true, -- Restore window options to previous values when leaving an oil buffer restore_win_options = true, -- Skip the confirmation popup for simple operations @@ -331,12 +332,3 @@ If you don't need those features specifically, check out the alternatives listed - [vidir](https://github.com/trapd00r/vidir): Never personally used, but might be the first plugin to come up with the idea of editing a directory like a buffer. There's also file trees like [neo-tree](https://github.com/nvim-neo-tree/neo-tree.nvim) and [nvim-tree](https://github.com/nvim-tree/nvim-tree.lua), but they're really a different category entirely. - -**Q: I don't need netrw anymore. How can I disable it?** - -**A:** Oil can fully replace netrw for local and ssh file browsing/editing, but keep in mind that netrw also supports rsync, http, ftp, and dav. If you don't need these other features, you can disable netrw with the following: - -```lua -vim.g.loaded_netrw = 1 -vim.g.loaded_netrwPlugin = 1 -``` diff --git a/doc/oil.txt b/doc/oil.txt index bdca4d9..b04be3f 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -14,6 +14,9 @@ OPTIONS *oil-option > require("oil").setup({ + -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) + -- Set to false if you still want to use netrw. + default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns columns = { @@ -38,8 +41,6 @@ OPTIONS *oil-option conceallevel = 3, concealcursor = "n", }, - -- Oil will take over directory buffers (e.g. `vim .` or `:e src/` - default_file_explorer = true, -- Restore window options to previous values when leaving an oil buffer restore_win_options = true, -- Skip the confirmation popup for simple operations diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 305f377..a7163d4 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -1,4 +1,7 @@ local default_config = { + -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) + -- Set to false if you still want to use netrw. + default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns columns = { @@ -23,8 +26,6 @@ local default_config = { conceallevel = 3, concealcursor = "n", }, - -- Oil will take over directory buffers (e.g. `vim .` or `:e src/` - default_file_explorer = true, -- Restore window options to previous values when leaving an oil buffer restore_win_options = true, -- Skip the confirmation popup for simple operations diff --git a/lua/oil/init.lua b/lua/oil/init.lua index ef9a832..08200c0 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -846,8 +846,13 @@ M.setup = function(opts) end, { desc = "Open oil file browser on a directory", nargs = "*", complete = "dir" }) local aug = vim.api.nvim_create_augroup("Oil", {}) - if config.default_file_explorer and vim.fn.exists("#FileExplorer") then - vim.api.nvim_create_augroup("FileExplorer", { clear = true }) + if config.default_file_explorer then + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + -- If netrw was already loaded, clear this augroup + if vim.fn.exists("#FileExplorer") then + vim.api.nvim_create_augroup("FileExplorer", { clear = true }) + end end local patterns = {} @@ -967,24 +972,6 @@ M.setup = function(opts) end, }) end - if - vim.g.loaded_netrwPlugin ~= 1 - and not config.silence_netrw_warning - and config.default_file_explorer - then - vim.api.nvim_create_autocmd("FileType", { - desc = "Inform user how to disable netrw", - group = aug, - pattern = "netrw", - once = true, - callback = function() - vim.notify( - "If you expected an Oil buffer here, you may want to disable netrw (:help netrw-noload)\nSet `default_file_explorer = false` in oil.setup() to not take over netrw buffers, or `silence_netrw_warning = true` to disable this message.", - vim.log.levels.WARN - ) - end, - }) - end vim.api.nvim_create_autocmd("WinNew", { desc = "Restore window options when splitting an oil window", group = aug,