From 6ed1f17662b89e9ddf5a047b3c7540a329470e20 Mon Sep 17 00:00:00 2001 From: bdesforges Date: Mon, 25 Aug 2025 14:11:32 -0600 Subject: [PATCH] Update init.lua added save cursor position --- init.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/init.lua b/init.lua index bd972c5c..7dd4fb37 100644 --- a/init.lua +++ b/init.lua @@ -1012,5 +1012,65 @@ require('lazy').setup({ }, }) +-- adapted from https://github.com/ethanholz/nvim-lastplace/blob/main/lua/nvim-lastplace/init.lua +local ignore_buftype = { 'quickfix', 'nofile', 'help' } +local ignore_filetype = { 'gitcommit', 'gitrebase', 'svn', 'hgcommit' } + +local function run() + if vim.tbl_contains(ignore_buftype, vim.bo.buftype) then + return + end + + if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then + -- reset cursor to first line + vim.cmd [[normal! gg]] + return + end + + -- If a line has already been specified on the command line, we are done + -- nvim file +num + if vim.fn.line '.' > 1 then + return + end + + local last_line = vim.fn.line [['"]] + local buff_last_line = vim.fn.line '$' + + -- If the last line is set and the less than the last line in the buffer + if last_line > 0 and last_line <= buff_last_line then + local win_last_line = vim.fn.line 'w$' + local win_first_line = vim.fn.line 'w0' + -- Check if the last line of the buffer is the same as the win + if win_last_line == buff_last_line then + -- Set line to last line edited + vim.cmd [[normal! g`"]] + -- Try to center + elseif buff_last_line - last_line > ((win_last_line - win_first_line) / 2) - 1 then + vim.cmd [[normal! g`"zz]] + else + vim.cmd [[normal! G'"]] + end + end +end + +vim.api.nvim_create_autocmd('BufRead', { + 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, +}) + +- + + -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et