nvim-lua / kickstart.nvim

A launch point for your personal nvim configuration
MIT License
18.9k stars 21.67k forks source link

Autocmd to restore cursor position #871

Closed voyeg3r closed 5 months ago

voyeg3r commented 5 months ago

local autocmd = vim.api.nvim_create_autocmd
local function augroup(name)
  return vim.api.nvim_create_augroup('sergio-lazyvim_' .. name, { clear = true })
end

autocmd('BufReadPost', {
  group = augroup 'restore_position',
  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(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)
      -- require('core.utils').flash_cursorline()
    end
  end,
  desc = 'Go to the last loc when opening a buffer',
})
feoh commented 5 months ago

Should this be a pull request instead? I don't understand what you're asking here.

dam9000 commented 5 months ago

There's a plugin that does this: https://github.com/mrcjkb/nvim-lastplace All you need to do is add it to the list of plugins:

    { 'mrcjkb/nvim-lastplace' },

I briefly tested it and it seems to work.

jakubreron commented 5 months ago

I feel like the snippet is too complicated, wouldn't this do the job?

vim.api.nvim_create_autocmd('BufRead', {
  pattern = '*',
  command = 'normal g\'"',
})
feoh commented 5 months ago

Given the existence of a plugin as @dam9000 cited, I'm closing this. Thanks for your contribution!

ChillerDragon commented 5 months ago

There's a plugin that does this: https://github.com/mrcjkb/nvim-lastplace All you need to do is add it to the list of plugins:

    { 'mrcjkb/nvim-lastplace' },

I briefly tested it and it seems to work.

It seems to break the goto definition flow. Or <leader>sg.

I have been using https://github.com/rmagatti/auto-session so far to also restore the open buffers. But both plugins break all goto line number workflows for me. (search word, goto function definition).

Sadly this one also seems to overwrite the line I want to goto when I go to the definition of a function.

vim.api.nvim_create_autocmd('BufRead', {
  pattern = '*',
  command = 'normal g\'"',
})
ChillerDragon commented 5 months ago

Can we reopen the issue @feoh or should I create a new one?

feoh commented 5 months ago

please don't do either. If you're having issues with a third-party plug-in, please file an issue against that plug-ins repository and not here. Kickstart cannot be all things to all people.

ChillerDragon commented 5 months ago

please don't do either. If you're having issues with a third-party plug-in, please file an issue against that plug-ins repository and not here. Kickstart cannot be all things to all people.

Yes that makes sense. But restoring cursor position is a common thing isn't it? Imo that should be a solved problem by kickstart. And as of right now the cursor restore options do not seem to work with kickstart.