Skip to content

FEATURE: Better netrw hijacking #1247

Closed
@Aonodensetsu

Description

@Aonodensetsu

Did you check the docs?

  • I have read all the docs.

Is your feature request related to a problem? Please describe.

When lazy-loading plugins, neo-tree cannot be lazy-loaded if you want it to hijack netrw properly (upon opening neovim with a folder argument). I made a fix that I want to share.

Describe the solution you'd like.

My fix allows full lazy-loading, keeps netrw for remote files (since I don't think neo-tree supports those). I would like for it to be integrated into neo-tree, as I feel it aligns with the goal of 'UX good'.

-- vim.bo.filetype is `:set filetype`, which netrw is nice enough to use
-- vim.b.netrw_method is `let b:netrw_method` - nil when editing local files (`:h netrw-fixup` for others)
if vim.bo.filetype == 'netrw' and vim.b.netrw_method == nil then
  -- wait until netrw loads fully, trying to close before will not work
  vim.defer_fn(function()
    -- the enew is required, if netrw is replaced directly it can leave some text below the tree if the tree is short
    -- (at least on my raspberry pi, may only be a problem on slow systems?)
    -- vim.b.netrw_curdir (`let b:netrw_curdir`) is the currently open folder
    -- which is what the user wants, since we replace netrw before any interaction
    vim.cmd('enew | Neotree current dir=' .. vim.b.netrw_curdir)
  end, 0)
end

Additional Context

Example configuration in lazy:

{
  'nvim-neo-tree/neo-tree.nvim',
  ft = 'netrw', -- load when netrw opens to replace it
  cmd = 'Neotree', -- load when calling keymaps
  init = function()
    -- make keymaps here to load them before the plugin itself
    -- they need to be in VimScript to not load the plugin prematurely
    -- they also need to begin with something specified in `cmd`
    -- (for this plugin, :Neotree has all of them i think?)
  end,
  config = function()
    require'neo-tree'.setup{
      filesystem = {
        hijack_netrw_behavior = 'open_current'
      }
    }
    if vim.bo.filetype == 'netrw' and vim.b.netrw_method == nil then
      vim.defer_fn(function()
        vim.cmd('enew | Neotree current dir=' .. vim.b.netrw_curdir)
      end, 0)
    end
  end
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions