Snippets

Sergio Luiz Araújo Silva Restore cursor on nvim

Created by Sergio Araújo last modified
local augroup = vim.api.nvim_create_augroup
local autocmd = vim.api.nvim_create_autocmd

local Cursor = augroup("Cursor", { clear = true })
autocmd({ "BufReadPost" }, {
  pattern = { "*" },
  group = Cursor,
  callback = function()
    local ft = vim.opt_local.filetype:get()
    if ft:match("commit") or ft:match("rebase") then
      return
    end
    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)
      vim.api.nvim_feedkeys('zz', 'n', true)
    end
  end,
})

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.