From a56377c11e51b4fc81df1898cc685032049b8c49 Mon Sep 17 00:00:00 2001 From: agryphus Date: Sun, 12 Jul 2026 16:11:50 -0400 Subject: [PATCH] fix: terminal snapping back to bottom on tabenter --- lua/andrew/remap.lua | 12 ------------ lua/andrew/set.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lua/andrew/remap.lua b/lua/andrew/remap.lua index 2d9445f..58a88c4 100644 --- a/lua/andrew/remap.lua +++ b/lua/andrew/remap.lua @@ -116,15 +116,3 @@ vim.keymap.set("n", "", "tab split", { desc = "Clone window in new vim.keymap.set("n", "", "termi", { desc = "Terminal" }) -vim.api.nvim_create_autocmd("TabEnter", { - callback = function() - local bufnr = vim.api.nvim_get_current_buf() - local buftype = vim.api.nvim_buf_get_option(bufnr, "buftype") - - if buftype == "terminal" then - -- Enter Terminal-Job mode - vim.api.nvim_feedkeys("i", "n", false) - end - end, -}) - diff --git a/lua/andrew/set.lua b/lua/andrew/set.lua index 4923e9f..f6430b1 100644 --- a/lua/andrew/set.lua +++ b/lua/andrew/set.lua @@ -61,3 +61,43 @@ endfunction autocmd TermOpen * call TerminalSettings() ]]) +-- Terminal window scroll positions by default are not saved when switching tabs +local termview = {} + +vim.api.nvim_create_autocmd({ "BufLeave", "WinLeave" }, { + pattern = "term://*", + callback = function() + termview[vim.api.nvim_get_current_buf()] = vim.fn.winsaveview() + end, +}) + +vim.api.nvim_create_autocmd({ "BufEnter", "WinEnter" }, { + pattern = "term://*", + callback = function() + local view = termview[vim.api.nvim_get_current_buf()] + if view then + vim.fn.winrestview(view) + end + end, +}) + +vim.api.nvim_create_autocmd("TabEnter", { + callback = function() + local bufnr = vim.api.nvim_get_current_buf() + local buftype = vim.api.nvim_buf_get_option(bufnr, "buftype") + if buftype ~= "terminal" then + return + end + local view = termview[bufnr] + if view then + vim.fn.winrestview(view) + -- only auto-insert if they were at the bottom (i.e. not deliberately scrolled up) + local last_line = vim.api.nvim_buf_line_count(bufnr) + if view.lnum >= last_line then + vim.api.nvim_feedkeys("i", "n", false) + end + else + vim.api.nvim_feedkeys("i", "n", false) + end + end, +})