Open tigion opened 1 year ago
Plugins:
Autocmd:
vim.api.nvim_create_autocmd("BufReadPost", {
group = augroup("last_loc"),
callback = function()
local exclude = { "gitcommit" }
local buf = vim.api.nvim_get_current_buf()
if vim.tbl_contains(exclude, vim.bo[buf].filetype) then
return
end
local mark = vim.api.nvim_buf_get_mark(buf, '"')
local lcount = vim.api.nvim_buf_line_count(buf)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
local lastplace = vim.api.nvim_create_augroup("LastPlace", {})
vim.api.nvim_clear_autocmds({ group = lastplace })
vim.api.nvim_create_autocmd("BufReadPost", {
group = lastplace,
pattern = { "*" },
desc = "remember last cursor place",
callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"')
local lcount = vim.api.nvim_buf_line_count(0)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
local autocmd = vim.api.nvim_create_autocmd
--function(autocmd, group_id)
autocmd("BufReadPre", {
callback = function()
autocmd("FileType", {
pattern = "<buffer>",
once = true,
callback = function()
local filetype = vim.opt.filetype:get()
if (filetype == "gitcommit" or filetype == "gitrebase") then
vim.cmd([[ exe 'normal! gg' ]])
elseif vim.fn.line("'") > 1 and vim.fn.line("'") <= vim.fn.line("$") then
vim.cmd([[ exe 'normal! g`"' ]])
end
end,
})
end,
group = group_id,
pattern = "*",
})
--end
local cmd = vim.cmd
function save_cursor_position()
-- Save the last cursor position.
cmd([[ if @% !~# '\.git[\/\\]COMMIT_EDITMSG$' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif ]])
end
-- https://gitlab.com/FOSSilized_Daemon/dotfiles/-/blob/main/src/dotfiles/home/.config/nvim/lua/autocmd.lua
create_autocmd('BufReadPost', {
callback = function()
save_cursor_position()
end,
group = read_group })
Currently nvim-lastplace works, but may break on future Neovim releases.