diff --git a/modules/terminal-life/nvim/default.nix b/modules/terminal-life/nvim/default.nix index 5ab04e05..136a7275 100644 --- a/modules/terminal-life/nvim/default.nix +++ b/modules/terminal-life/nvim/default.nix @@ -193,6 +193,7 @@ in { (builtins.readFile ./ui.vim) (builtins.readFile ./quickfixopenall.vim) (builtins.readFile ./lsp.vim) + (builtins.readFile ./lastplace.lua) '' " fzf with file preview command! -bang -nargs=? -complete=dir Files diff --git a/modules/terminal-life/nvim/init.vim b/modules/terminal-life/nvim/init.vim index 5519140c..f19302db 100644 --- a/modules/terminal-life/nvim/init.vim +++ b/modules/terminal-life/nvim/init.vim @@ -106,12 +106,6 @@ imap (fzf-complete-line) " Clear quickfix shortcut nmap c :ccl -" Remember cursor position -" Vim jumps to the last position when reopening a file -if has("autocmd") - au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif -endif - nmap - :NnnPicker % nmap n :NnnPicker % nmap N :NnnPicker diff --git a/modules/terminal-life/nvim/lastplace.lua b/modules/terminal-life/nvim/lastplace.lua new file mode 100644 index 00000000..79a1932d --- /dev/null +++ b/modules/terminal-life/nvim/lastplace.lua @@ -0,0 +1,47 @@ +lua < 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`"]], bang = true} + -- Try to center + elseif buff_last_line - last_line > ((win_last_line - win_first_line) / 2) - 1 then + vim.cmd.normal{[[g`"zz]], bang = true} + else + vim.cmd.normal{[[G'"]], bang = true} + end + end + end + +vim.api.nvim_create_autocmd({'BufWinEnter', 'FileType'}, { + group = vim.api.nvim_create_augroup('nvim-lastplace', { clear = true }), + callback = run +}) +EOF