Add autocmd to restore the cursor position across sessions

This commit is contained in:
Kenneth Benzie 2024-03-05 21:30:54 +00:00
parent a4b3d67216
commit b03171b93a
2 changed files with 25 additions and 0 deletions

View File

@ -1,6 +1,7 @@
require('settings')
require('mappings')
require('netrw')
require('autocmds')
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then

24
lua/autocmds.lua Normal file
View File

@ -0,0 +1,24 @@
-- Create a group to namespace autocmds
local group = vim.api.nvim_create_augroup('benie', { clear = true })
-- Restore cursor position across sessions
-- https://github.com/neovim/neovim/issues/16339#issuecomment-1457394370
vim.api.nvim_create_autocmd('BufRead', {
group = group,
callback = function(opts)
vim.api.nvim_create_autocmd('BufWinEnter', {
once = true,
buffer = opts.buf,
callback = function()
local ft = vim.bo[opts.buf].filetype
local last_known_line = vim.api.nvim_buf_get_mark(opts.buf, '"')[1]
if not (ft:match('commit') and ft:match('rebase'))
and last_known_line > 1
and last_known_line <= vim.api.nvim_buf_line_count(opts.buf)
then
vim.api.nvim_feedkeys([[g`"]], 'nx', false)
end
end
})
end
})