Skip to content

Vim(normal):Can't re-enter normal mode from terminal mode #3018

Closed
@shy-robin

Description

@shy-robin

Description

When I focus on nvim tree panel, I open vim-floaterm and switch floaterm, it occurs error:

Error executing vim.schedule lua callback: ...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:201: Error executing lua: vim/_editor.lua:0: nvim_exec2(): Vim(normal):Can't re-enter normal mode from terminal mode
stack traceback:
	[C]: in function 'nvim_exec2'
	vim/_editor.lua: in function 'cmd'
	...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:206: in function <...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:201>
	[C]: in function 'nvim_buf_call'
	...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:201: in function <...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:200>
stack traceback:
	[C]: in function 'nvim_buf_call'
	...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:201: in function <...n/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:200>
2024-12-06.23.00.29.1.mov

I have read the source code of nvim-tree.lua and I found the reason. When I switch floaterm, the cursor will focus on nvim-tree panel quickly and the mode now is the terminal mode. Because I config centralize_selection = true, it will use zz to centralize selection but now is terminal mode, cannot use zz command and it causes error.

The relevant source code is as follows:

  if opts.view.centralize_selection then
    create_nvim_tree_autocmd("BufEnter", {
      pattern = "NvimTree_*",
      callback = function()
        vim.schedule(function()
          vim.api.nvim_buf_call(0, function()
            vim.cmd([[norm! zz]]) -- -> causes error in terminal mode
          end)
        end)
      end,
    })
  end

So I think we should first determine whether the mode is terminal mode or not, like this:

  if opts.view.centralize_selection then
    create_nvim_tree_autocmd("BufEnter", {
      pattern = "NvimTree_*",
      callback = function()
        vim.schedule(function()
          vim.api.nvim_buf_call(0, function()
            local is_term_mode = vim.api.nvim_get_mode().mode == "t"
            -- return if the mode is terminal mode
            if is_term_mode then
              return
            end

            vim.cmd([[norm! zz]])
          end)
        end)
      end,
    })
  end

Neovim version

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1720049189

Operating system and version

macOS 15.1.1

Windows variant

No response

nvim-tree version

ca7c4c3

Clean room replication

empty

Steps to reproduce

  1. use <leader>e to focus nvim tree panel
  2. use <C-o> to open vim-floaterm
  3. use <C-h/l> to switch vim-floaterm
  4. it occurs error

Expected behavior

Not display error when I switch vim-floaterm.

Actual behavior

It occurs error when I switch vim-floaterm.

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