1 local autocmd = vim.api.nvim_create_autocmd
3 -- Returns a "clear = true" augroup
4 local function augroup(name)
5 return vim.api.nvim_create_augroup(name, { clear = true })
8 autocmd("BufReadPost", {
9 desc = "Restore cursor position on file reopen, excluding some filetypes",
10 group = augroup("RestoreCursorPosition"),
12 local exclude = { "gitcommit" }
13 local buf = vim.api.nvim_get_current_buf()
14 if vim.tbl_contains(exclude, vim.bo[buf].filetype) then
18 local mark = vim.api.nvim_buf_get_mark(buf, '"')
19 local line_count = vim.api.nvim_buf_line_count(buf)
20 if mark[1] > 0 and mark[1] <= line_count then
21 pcall(vim.api.nvim_win_set_cursor, 0, mark)
22 vim.api.nvim_feedkeys("zvzz", "n", true)