neovim: fix remember cursor position

This commit is contained in:
teutat3s 2024-04-22 14:27:58 +02:00
parent 130c915612
commit bcfd701a6a
Signed by: teutat3s
GPG key ID: 4FA1D3FA524F22C1
3 changed files with 48 additions and 6 deletions

View file

@ -193,6 +193,7 @@ in {
(builtins.readFile ./ui.vim) (builtins.readFile ./ui.vim)
(builtins.readFile ./quickfixopenall.vim) (builtins.readFile ./quickfixopenall.vim)
(builtins.readFile ./lsp.vim) (builtins.readFile ./lsp.vim)
(builtins.readFile ./lastplace.lua)
'' ''
" fzf with file preview " fzf with file preview
command! -bang -nargs=? -complete=dir Files command! -bang -nargs=? -complete=dir Files

View file

@ -106,12 +106,6 @@ imap <c-x><c-l> <plug>(fzf-complete-line)
" Clear quickfix shortcut " Clear quickfix shortcut
nmap <Leader>c :ccl<CR> nmap <Leader>c :ccl<CR>
" 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 %<CR> nmap - :NnnPicker %<CR>
nmap <leader>n :NnnPicker %<CR> nmap <leader>n :NnnPicker %<CR>
nmap <leader>N :NnnPicker<CR> nmap <leader>N :NnnPicker<CR>

View file

@ -0,0 +1,47 @@
lua <<EOF
-- from https://github.com/neovim/neovim/issues/16339#issuecomment-1348133829
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', bang = true}
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`"]], 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'"<c-e>]], 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