nvim-tree / nvim-tree/nvim-tree.lua

Previous window is not selected unless :NvimTreeFocus is used

Open
#2,262 10 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

awaiting feedback
Dominant language
Lua
Stars
8.6k
Forks
639
Avg merge
1d 14h
Merged PRs (30d)
2

Description

Description

From https://github.com/nvim-tree/nvim-tree.lua/issues/1985#issuecomment-1582896807

Is there really no way to open to the window that was previously active before selecting the nvim-tree window (regardless on navigation method)?

I have the same problem as @paulrouget, however I often use the mouse to start interacting with nvim-tree (to open a file, for instance). If I don't use :NvimTreeFocus, then it opens the file in the last window to have previously used :NvimTreeFocus (or presumably have been created by nvim-tree).

Would it be possible to use the lua equivalent of :winnr(#) (if it exists)?

Would it be possible to track the previous window by an event that fires every time you leave a window? Like maybe the WinLeave autocmd event? We could add another WinLeave autocmd similar to the one defined here, but using a negated pattern to ignore unwanted buffers like NvimTree_*.


TL;DR

This plugin can only track the "previous active window" when it's focused via :NvimTreeFocus. If the nvim-tree is focused by any other method, it will still refer to the window where :NvimTreeFocus was lat called, not the actual last window.

Neovim version
NVIM v0.9.0
Build type: Release
LuaJIT 2.1.0-beta3
Operating system and version

Linux 6.2.12-zen1-1-zen, MacOS 13.4

nvim-tree version

Latest master (034511714bacfadc5008e49f73fcef67e5613840 as of testing)

Minimal config
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup {
    {
      "wbthomason/packer.nvim",
      "nvim-tree/nvim-tree.lua",
      "nvim-tree/nvim-web-devicons",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  }
end
if vim.fn.isdirectory(install_path) == 0 then
  print "Installing nvim-tree and dependencies."
  vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup({
    actions = {
      open_file = {
        window_picker = {
          enable = false
        },
      },
    },
  })
end
Steps to reproduce

Setup

Open nvim-tree and open at least 2 files in splits (vertical/horizontal doesn't seem to matter). In this example I'll open file1, file2, and file3 in vertical splits in order (1 on the left, 3 on the right).

  1. nvim -nu /tmp/nvt-min.lua
  2. :NvimTreeOpen
  3. Select file1 and press <C-v>
  4. :NvimTreeFocus
  5. Select file2 and press <C-v>
  6. :NvimTreeFocus
  7. Select file3 and press <C-v>

The bug

  1. With window 3 selected (as it should be after the last step above), focus nvim-tree with :NvimTreeFocus
  2. Open another file with <CR>. The file opens in window 3, as it was the last window selected.
  3. Focus window 1 (with file1 open) with any method of your choice (mouse, <C-w>h, whatever)
  4. Focus nvim-tree this time with a method that doesn't involve calling :NvimTreeFocus, like clicking the window with the mouse, or using <C-w>h to move the cursor to the nvim-tree window.
  5. Open a file that is not currently open in a window with <CR>
Expected behavior

I would expect the file to be opened in window 1 (where file1 used to be), as it was the last window selected.

Actual behavior

The file is opened in window 3 (where file3 was), as it was the window selected when :NvimTreeFocus was last called.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the WinLeave autocmd referenced in lua/nvim-tree.lua around lines 320-327, then trace how the previous window is recorded and used when opening files. Reproduce the mouse or navigation case from the issue and compare it with :NvimTreeFocus. Done means opening a new file after non-command focus uses the actual previously active window.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.