akinsho / toggleterm.nvim

A neovim lua plugin to help easily manage multiple terminal windows
GNU General Public License v3.0
4.21k stars 170 forks source link

[BUG] `:tabnew %` on a toggleterm buffer causes it to lose cursor position #516

Closed ColinKennedy closed 7 months ago

ColinKennedy commented 9 months ago

Is there an existing issue for this?

Current Behavior

If you scroll up in terminal-normal mode and then call :tabnew %, a regular terminal keeps the same cursor position in the new tab. However a toggleterm does not.

Several plugins spawn new tabs from buffers such as :ZoomWinTabToggle from https://github.com/troydm/zoomwintab.vim. (To be specific, that plugin uses :tab sb bufnr() but it's the same issue)

Expected Behavior

Creating a new tab from a toggleterm buffer continues to use the same cursor position.

Steps To Reproduce

Using this reproduction file

Click to expand ```lua -- DO NOT change the paths and don't remove the colorscheme local root = vim.fn.fnamemodify("./.repro", ":p") -- set stdpaths to use .repro for _, name in ipairs({ "config", "data", "state", "cache" }) do vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name end -- bootstrap lazy local lazypath = root .. "/plugins/lazy.nvim" if not vim.loop.fs_stat(lazypath) then vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch", "https://github.com/folke/lazy.nvim.git", lazypath, }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { {'akinsho/toggleterm.nvim', version = "*", config = true}, } require("lazy").setup(plugins, { root = root .. "/plugins", }) ```
  1. Save the lua above to /tmp/minimal_reproduction.lua
  2. Load neovim -u /tmp/minimal_reproduction.lua
  3. Call :ToggleTerm
  4. call cat /tmp/some_large_file.txt (A file with a lot of text in it)
  5. Press <C-\>C-n> to escape TERMINAL mode back into NORMAL mode in the same terminal buffer
  6. Call :tabnew %

The created tab + toggleterm buffer will have its position at the bottom of the buffer, rather than wherever you last left it.

See this video for a demonstration

https://github.com/akinsho/toggleterm.nvim/assets/10103049/da883d1a-fbdf-47b2-99ec-eebe299c023f

There's also a second issue where calling :set showtabline=0 before calling :tabclose will cause the original toggleterm buffer's cursor position to be reset. But with this bug in play, it's hard to tell if that's a separate issue or a different symptom to the same issue as this.

Environment

- OS: CentOS 7
- neovim version: `NVIM v0.9.4 - Build type: Release - LuaJIT 2.1.1692716794`
- Shell: bash

Anything else?

No response

ColinKennedy commented 7 months ago

The first issue seems to be caused by auto_scroll. The second issue I cannot reproduce anymore. This issue can close