Restore cursor position after opening file

This commit is contained in:
Josh Holtrop 2026-06-25 01:54:34 -04:00
parent 3de823f924
commit 7bcd0d480b

View File

@ -43,6 +43,24 @@ vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
end, 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 -- Shortcuts
vim.cmd([[map <C-s> :FzfLua combine pickers=buffers,git_files<CR>]]) vim.cmd([[map <C-s> :FzfLua combine pickers=buffers,git_files<CR>]])
vim.cmd([[map f<C-s> :FzfLua combine pickers=buffers,files<CR>]]) vim.cmd([[map f<C-s> :FzfLua combine pickers=buffers,files<CR>]])