Skip to content

Creating a file in deeply nested folders resets nvim-tree scroll position #3164

Open
@arlazarov

Description

@arlazarov

Description

Bug Description

When creating a new file in nvim-tree using the a key, the prompt opens as expected. However, as I type the file name, the cursor keeps jumping back to the beginning of the input, making it impossible to enter the full file name.

Neovim version

NVIM v0.11.0
Build type: Release
LuaJIT 2.1.1744318430

Operating system and version

Macos

Windows variant

No response

nvim-tree version

HEAD

Clean room replication

return {
	"nvim-tree/nvim-tree.lua",
	commit = "HEAD",

	dependencies = {
		"nvim-tree/nvim-web-devicons",
		"ryanoasis/vim-devicons",
		"folke/which-key.nvim",
	},
	config = function()
		local api = require("nvim-tree.api")

		-- Toggle с <Tab>
		vim.keymap.set(
			"n",
			"<Tab>",
			api.tree.toggle,
			{ desc = "Toggle NvimTree" }
		)

		require("which-key").add({
			{
				"<leader>e",
				function()
					vim.cmd("cd %:p:h")
					vim.cmd("pwd")
					api.tree.toggle({
						path = vim.fn.expand("%:p:h"),
						find_file = false,
					})
				end,
				desc = "Nvim-tree: setup",
			},
		})

		local function my_on_attach(bufnr)
			local function opts(desc)
				return {
					desc = "NvimTree: " .. desc,
					buffer = bufnr,
					noremap = true,
					silent = true,
					nowait = true,
				}
			end

			api.config.mappings.default_on_attach(bufnr)

			vim.keymap.set("n", "<C-o>", function()
				local node =
					require("nvim-tree.api").tree.get_node_under_cursor()
				if not node or not node.absolute_path then
					vim.notify("No file under cursor", vim.log.levels.WARN)
					return
				end
				vim.fn.jobstart(
					{ "open", "-R", node.absolute_path },
					{ detach = true }
				)
			end, opts("Reveal in Finder"))

			vim.keymap.set("n", "<Tab>", api.tree.toggle, opts("Toggle"))
			vim.keymap.set(
				"n",
				"<CR>",
				api.node.open.preview,
				opts("Preview file")
			)
			vim.keymap.set("n", "s", api.node.open.horizontal, opts("Split"))
			vim.keymap.set("n", "v", api.node.open.vertical, opts("VSplit"))
			vim.keymap.set("n", "p", api.tree.change_root_to_parent, opts("Up"))
			vim.keymap.set(
				"n",
				"c",
				api.tree.collapse_all,
				opts("Collapse all")
			)
			vim.keymap.set(
				"n",
				"gm",
				api.marks.bulk.move,
				opts("Bulk Move Marked Files")
			)
		end

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

		require("nvim-tree").setup({
			on_attach = my_on_attach,
			disable_netrw = true,
			hijack_cursor = true,
			update_focused_file = {
				enable = true,
				update_cwd = false,
			},
			renderer = {
				indent_markers = {
					enable = true,
				},
				icons = {
					webdev_colors = true,
					show = {
						file = true,
						folder = true,
						folder_arrow = true,
						git = true,
					},
				},
			},
			filters = {
				dotfiles = true,
				custom = { "node_modules" },
			},
			diagnostics = {
				enable = true,
				show_on_dirs = true,
			},
			git = {
				enable = true,
				ignore = true,
				show_on_dirs = true,
			},
			view = {
				width = 30,
				side = "left",
				preserve_window_proportions = true,
			},
		})
	end,
}

Steps to reproduce

  1. Open nvim-tree
  2. Press a to create a file
  3. Start typing something like: Users/antonarlazarov/GG Freight Services/Daily Notes/2025/July/test.md
  4. After typing a few characters, the cursor jumps to the start of the line, and continues to reset on every keystroke.

Expected behavior

The cursor should remain at the end of the input as the user types, allowing a full path or long file name to be entered without interruption.

Actual behavior

After typing a few characters, the cursor jumps to the start of the line, and continues to reset on every keystroke.

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidIssue not filled as per issue template

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions