diff --git a/lua/my_autocommands.lua b/lua/my_autocommands.lua index 43a7b6b..bc057ac 100644 --- a/lua/my_autocommands.lua +++ b/lua/my_autocommands.lua @@ -61,6 +61,24 @@ api.nvim_create_autocmd({ 'FocusLost' }, { command = [[wshada]] }) -- { command = [[if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g`\"" | endif]] } -- ) +-- When editing a file, always jump to the last known cursor position. +-- Don't do it when the position is invalid, when inside an event handler +-- (happens when dropping a file on gvim) and for a commit message (it's +-- likely a different one than last time). +local userGrp = api.nvim_create_augroup('UserGroup', { clear = true }) +vim.api.nvim_create_autocmd('BufReadPost', { + group = userGrp, + callback = function(args) + local valid_line = vim.fn.line([['"]]) >= 1 and vim.fn.line([['"]]) < vim.fn.line('$') + local not_commit = vim.b[args.buf].filetype ~= 'commit' + + if valid_line and not_commit then + vim.cmd([[normal! g`"]]) + end + end, +}) + + -- Check if we need to reload the file when it changed api.nvim_create_autocmd({ 'FocusGained', 'BufEnter' }, { command = [[if !bufexists("[Command Line]") | checktime | endif]] })