neo-tree.nvim icon indicating copy to clipboard operation
neo-tree.nvim copied to clipboard

BUG: Need to open file twice when opening neovim with "nvim ."

Open jakkan opened this issue 1 year ago • 4 comments

Did you check docs and existing issues?

  • [X] I have read all the docs.
  • [X] I have searched the existing issues.
  • [X] I have searched the existing discussions.

Neovim Version (nvim -v)

0.9.4

Operating System / Version

Archlinux / linux 6.5.7.arch1-1

Describe the Bug

When I open neovim with "nvim ." I need to open a file twice for it to actually be opened in a buffer.

Screenshots, Traceback

No response

Steps to Reproduce

  1. nvim .
  2. e to open neotree
  3. Enter to open a file
  4. The file contents flashes in the buffer and then dissapears.
  5. <C-w><C-w> to focus neotree again
  6. Enter to oten file
  7. File contents are opened into buffer

Expected Behavior

  1. nvim .
  2. e to open neotree
  3. Enter to open a file
  4. File contents are opened into buffer

Your Configuration

vim.g.loaded_netrwPlugin = 1
vim.g.loaded_netrw = 1

vim.g.mapleader = ' '

local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system {
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  }
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup({
  {
    'nvim-neo-tree/neo-tree.nvim',
    branch = 'v3.x',
    dependencies = {
      'nvim-lua/plenary.nvim',
      'nvim-tree/nvim-web-devicons', -- not strictly required, but recommended
      'MunifTanjim/nui.nvim',
    },
  },
}, {})

vim.keymap.set(
  'n',
  '<leader>e',
  ':Neotree<CR>'
)

jakkan avatar Oct 12 '23 21:10 jakkan

It was solved by setting hijack_netrw_behavior = "disabled":

return {
  'nvim-neo-tree/neo-tree.nvim',
  branch = 'v3.x',
  dependencies = {
    'nvim-lua/plenary.nvim',
    'nvim-tree/nvim-web-devicons', -- not strictly required, but recommended
    'MunifTanjim/nui.nvim',
  },
  config = function()
    require("neo-tree").setup({
       filesystem = {
         hijack_netrw_behavior = "disabled"
       }
     })
  end,
}

jakkan avatar Oct 16 '23 11:10 jakkan

Maybe related: #907?

This is caused by neo-tree lazy-loaded by lazy.nvim and only been invoked after you pressed <Leader>e with the keys keyword.

The most intuitive way to solve this issue would be to load neo-tree on startup when nvim ..

return {
  "nvim-neo-tree/neo-tree.nvim",
  dependencies = {
    { "MunifTanjim/nui.nvim" },
    { "nvim-tree/nvim-web-devicons" },
  },
  cmd = { "Neotree" },
  keys = {
    { "<Leader>e", "<Cmd>Neotree toggle<CR>", remap = false, silent = true, desc = "<Cmd>Neotree toggle<CR>" },
  },
  init = function()
    vim.g.neo_tree_remove_legacy_commands = 1
    ---@type string[]
    local argv = vim.fn.argv() or {} ---@diagnostic disable-line
    if vim.tbl_contains(argv, ".") then -- load neo-tree when `nvim .`
      vim.schedule(function()
        pcall(require, "neo-tree")
      end)
    end
  end,
  opts = {
    -- ..
  },
}

With the above configuration, you don't need to hijack_netrw_behavior = "disabled" as well.

pysan3 avatar Oct 18 '23 06:10 pysan3

Does the above solution work for you @jakkan ?

pysan3 avatar Nov 12 '23 10:11 pysan3

Thanks for the suggested solution above. Unfortunately, it doesn't seem to work for me. However, using hijack_netrw_behavior = "disabled" still works well. Given this, I think we can consider this issue resolved.

jakkan avatar Nov 20 '23 14:11 jakkan