Skip to content

list: save and restore cursor positions - #533

Open
davvid wants to merge 3 commits into
ThePrimeagen:harpoon2from
davvid:save-cursor-position
Open

davvid wants to merge 3 commits into
ThePrimeagen:harpoon2from
davvid:save-cursor-position

Conversation

@davvid

@davvid davvid commented Mar 11, 2024

Copy link
Copy Markdown
Contributor

Harpoon was not remembering the cursor position after quitting nvim.
Ensure that the cursor information is always saved and applied so that
we get back to exactly where we last were in the harpooned file.

Closes: #441

@davvid
davvid changed the base branch from master to harpoon2 March 11, 2024 02:14
@davvid
davvid force-pushed the save-cursor-position branch 4 times, most recently from 56f728d to 3d45f6f Compare March 11, 2024 04:40
@romado77

Copy link
Copy Markdown

@ThePrimeagen could you please review this PR? It addresses a long-awaited fix.

@maxrzaw

maxrzaw commented Apr 13, 2024

Copy link
Copy Markdown

This seems like an improvement, but doesn't appear to work unless you first navigate to the file using harpoon. I have tested this with sync_on_ui_close both true and false.

For example:

  1. vim A.txt
  2. Add mark on line 5
  3. Quit
  4. vim A.txt
  5. Move to line 10
  6. Quit
  7. vim B.txt
  8. navigate to A.txt in harpoon
  9. Should be on line 10 and not line 5

Adding this autocmd appears to fixe this issue with the default config.

        vim.api.nvim_create_autocmd({ "QuitPre" }, {
            pattern = "*",
            callback = function()
                local bufnr = vim.api.nvim_get_current_buf()
                local path = vim.api.nvim_buf_get_name(bufnr)
                for _, it in ipairs(Harpoon:list().items) do
                    local value = it.value
                    if value == path then
                        Harpoon:list():append() -- now Harpoon:list():add()
                        break
                    end
                end
            end,
        })

However, I still have an issue with the position not updating when switching between files (not using harpoon) and then navigating to the file using harpoon. Easy example of this is make a mark, move down, go to alternate file, go back using harpoon and you will be where the mark is and not where you last left off. I guess this is a different issue since it isn't while quitting, but it seems similar.

@ThePrimeagen

Copy link
Copy Markdown
Owner

I have done a lot of work on getting cursors to save constantly. The latest update does a distributed file style based on settings key, this allows saving upon every change

I would first make sure the latest harpoon2 still has the issue you're looking for

@maxrzaw

maxrzaw commented Apr 13, 2024

Copy link
Copy Markdown

@ThePrimeagen I just checked with latest, harpoon2 does still have the issue.

I was able to update the changes @davvid made to work with the latest harpoon2. Those changes along with the now much simpler autocmd seem to be working.

        vim.api.nvim_create_autocmd({ "QuitPre" }, {
            pattern = "*",
            callback = function()
                -- Do this for all your lists
                Harpoon:list():sync_cursor()
            end,
        })

I tried putting it in harpoon/init.lua and using _for_each_list, but it did not work.
The problem is that _for_each_list does not run callbacks for any lists when I open up a file, move around, but never interact with harpoon at all.

my updated branch https://github.com/maxrzaw/harpoon/tree/sync-cursor seems to be working. There might be some redundancies with the combination of the new changes to harpoon2 and the changes from this PR.

@davvid
davvid force-pushed the save-cursor-position branch 5 times, most recently from 99f2dbe to 8ce76c3 Compare April 13, 2024 23:27
@davvid

davvid commented Apr 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up @maxrzaw -- I've rebased this branch and updated to the latest harpoon2.

The out-of-bounds cursor tests had to be updated with these changes, which I suspect is okay since the results are getting updated to match the file bounds. That's probably worth another look.

@maxrzaw is the expectation that users will need to install the QuitPre autocmd themselves if they want a top-to-bottom fix?

The behavior is better for more scenarios with these changes at least. w/out these changes the cursor position is lost even when switching between two harpooned files. For example, move around in one file, harpoon to a 2nd file, and then harpoon back and the cursor will be in a different position from whence we came. It's in the correct position when using this branch.

Things :bNext and :bPrev do seem to reset the cursor, though, but at least this is better than it was before.

Curious, which scenario does the autocmd handle? I don't see any change in behavior when using save-cursor-position w/ and w/out the autocmd, though maybe that's because I have these options set?

        opts = {
            settings = {
                save_on_toggle = true,
                sync_on_ui_close = true,
            },
        },

@davvid
davvid force-pushed the save-cursor-position branch 2 times, most recently from e639279 to 9eea06a Compare April 14, 2024 07:57
@romado77

Copy link
Copy Markdown

I have done a lot of work on getting cursors to save constantly. The latest update does a distributed file style based on settings key, this allows saving upon every change

I would first make sure the latest harpoon2 still has the issue you're looking for

For me the position isn't being saved when I quit, regardless of whether I save the buffer or not. The only way I've found to make it work is by toggling the Harpoon list again (leader + h), selecting the same file from the list that's already open, and then quitting. Doing this saves the current position, allowing me to jump back to it when quitting Nvim and reopening the file via Harpoon. All other solutions don't work including AutoCmd suggested by @maxrzaw.
BTW, I'm on the latest commit.

@maxrzaw

maxrzaw commented Apr 15, 2024

Copy link
Copy Markdown

I have done a lot of work on getting cursors to save constantly. The latest update does a distributed file style based on settings key, this allows saving upon every change
I would first make sure the latest harpoon2 still has the issue you're looking for

For me the position isn't being saved when I quit, regardless of whether I save the buffer or not. The only way I've found to make it work is by toggling the Harpoon list again (leader + h), selecting the same file from the list that's already open, and then quitting. Doing this saves the current position, allowing me to jump back to it when quitting Nvim and reopening the file via Harpoon. All other solutions don't work including AutoCmd suggested by @maxrzaw. BTW, I'm on the latest commit.

Latest commit of which branch? I was using latest on harpoon2 with additional changes from this branch. https://github.com/maxrzaw/harpoon/tree/sync-cursor to be exact.

@maxrzaw

maxrzaw commented Apr 15, 2024

Copy link
Copy Markdown

Thanks for the heads-up @maxrzaw -- I've rebased this branch and updated to the latest harpoon2.

The out-of-bounds cursor tests had to be updated with these changes, which I suspect is okay since the results are getting updated to match the file bounds. That's probably worth another look.

@maxrzaw is the expectation that users will need to install the QuitPre autocmd themselves if they want a top-to-bottom fix?

The behavior is better for more scenarios with these changes at least. w/out these changes the cursor position is lost even when switching between two harpooned files. For example, move around in one file, harpoon to a 2nd file, and then harpoon back and the cursor will be in a different position from whence we came. It's in the correct position when using this branch.

Things :bNext and :bPrev do seem to reset the cursor, though, but at least this is better than it was before.

Curious, which scenario does the autocmd handle? I don't see any change in behavior when using save-cursor-position w/ and w/out the autocmd, though maybe that's because I have these options set?

        opts = {

            settings = {

                save_on_toggle = true,

                sync_on_ui_close = true,

            },

        },

These steps:

  1. vim A.txt
  2. Add mark on line 5
  3. Quit
  4. vim A.txt
  5. Move to line 10
  6. Quit
  7. vim B.txt
  8. navigate to A.txt in harpoon
  9. Should be on line 10 and not line 5

The key is that I open a harpooned file and move the cursor, but do nothing at all to interact with harpoon, then quit, this results in _for_each_list not working as expected.

@ThePrimeagen

Copy link
Copy Markdown
Owner

Let me double check everything, I think I know what happened.

I should get some time this week to go over everything

@romado77

Copy link
Copy Markdown

I have done a lot of work on getting cursors to save constantly. The latest update does a distributed file style based on settings key, this allows saving upon every change
I would first make sure the latest harpoon2 still has the issue you're looking for

For me the position isn't being saved when I quit, regardless of whether I save the buffer or not. The only way I've found to make it work is by toggling the Harpoon list again (leader + h), selecting the same file from the list that's already open, and then quitting. Doing this saves the current position, allowing me to jump back to it when quitting Nvim and reopening the file via Harpoon. All other solutions don't work including AutoCmd suggested by @maxrzaw. BTW, I'm on the latest commit.

Latest commit of which branch? I was using latest on harpoon2 with additional changes from this branch. https://github.com/maxrzaw/harpoon/tree/sync-cursor to be exact.

harpoon2

On branch harpoon2
Your branch is up to date with 'origin/harpoon2'.

nothing to commit, working tree clean

Comment thread lua/harpoon/list.lua Outdated
davvid added 3 commits January 2, 2026 00:19
Harpoon was not remembering the cursor position after quitting nvim.
Ensure that the cursor information is always saved and applied so that
we get back to exactly where we last were in the harpooned file.

Closes: ThePrimeagen#441
@davvid
davvid force-pushed the save-cursor-position branch from d30b567 to add8173 Compare January 2, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harpoon doesn't save cursor position after quit

4 participants