Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shy-robin opened this issue Dec 6, 2024 · 2 comments · Fixed by #3019
Closed

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

shy-robin opened this issue Dec 6, 2024 · 2 comments · Fixed by #3019
Labels
awaiting feedback bug Something isn't working

Comments

@shy-robin
Copy link
Contributor

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.

@alex-courtis
Copy link
Member

The fixs looks reasonable, however I can't reproduce the issue with the standard vim term.

Please provide a clean room replication with floaterm and your keybinds so that I can validate the fix.

@shy-robin
Copy link
Contributor Author

@alex-courtis
This is replication:

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
			"voldikss/vim-floaterm",
		},
		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({
		view = {
			centralize_selection = true,
		},
	})

	vim.keymap.set("n", "<C-e>", "<cmd>NvimTreeToggle<cr>")
	vim.keymap.set("n", "<C-o>", "<cmd>FloatermToggle<cr>")
	vim.keymap.set("t", "<C-o>", "<cmd>FloatermToggle<cr>")
	vim.keymap.set("n", "<C-n>", "<cmd>FloatermNew<cr>")
	vim.keymap.set("t", "<C-n>", "<cmd>FloatermNew<cr>")
	vim.keymap.set("n", "<C-h>", "<cmd>FloatermPrev<cr>")
	vim.keymap.set("t", "<C-h>", "<cmd>FloatermPrev<cr>")
end

-- UNCOMMENT this block for diagnostics issues, substituting pattern and cmd as appropriate.
-- Requires diagnostics.enable = true in setup.
--[[
vim.api.nvim_create_autocmd("FileType", {
  pattern = "lua",
  callback = function()
    vim.lsp.start {
      name = "my-luals",
      cmd = { "lua-language-server" },
      root_dir = vim.loop.cwd(),
    }
  end,
})
]]

The steps is as follows:

  1. use <C-e> to open nvim-tree
  2. use <C-o> to open vim-floaterm
  3. use <C-n> to create new vim-floaterm
  4. and it will occur error

alex-courtis added a commit that referenced this issue Dec 8, 2024
fix: Can't re-enter normal mode from terminal mode

Co-authored-by: Alexander Courtis <alex@courtis.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants