From 7bcd0d480bc2b46b3b44c5fd86a71850914fb76b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 25 Jun 2026 01:54:34 -0400 Subject: [PATCH] Restore cursor position after opening file --- plugin/holtrop.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugin/holtrop.lua b/plugin/holtrop.lua index cd9c084..3e8a4e7 100644 --- a/plugin/holtrop.lua +++ b/plugin/holtrop.lua @@ -43,6 +43,24 @@ vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { end, }) +-- Restore cursor position after opening a file +local cursor_group = vim.api.nvim_create_augroup("holtrop_restore_cursor", { clear = true }) + +vim.api.nvim_create_autocmd("BufReadPost", { + group = cursor_group, + pattern = "*", + callback = function() + -- Get the row and column of the last cursor position mark + local mark = vim.api.nvim_buf_get_mark(0, '"') + local lcount = vim.api.nvim_buf_line_count(0) + + -- If the mark is valid, jump to it, unless it's a git commit message + if mark[1] > 0 and mark[1] <= lcount and vim.bo.filetype ~= "gitcommit" then + pcall(vim.api.nvim_win_set_cursor, 0, mark) + end + end, +}) + -- Shortcuts vim.cmd([[map :FzfLua combine pickers=buffers,git_files]]) vim.cmd([[map f :FzfLua combine pickers=buffers,files]])