You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Alternate buffer not restoring cursor position with window splits #28
Sometimes I am editing a file a.txt and I want to reference two separate locations while editing. For this I usually split the window. I will also often switch between the alternate buffer b.txt for another reference. Under some circumstances (detailed below), this plugin prevents restoring the cursor to the last buffer position when switching alternate buffers. Here are two recordings:
In both cases I:
Open a.txt for editing. Note that the cursor starts on line 3 (the '" marks's location)
Use 'G' to view the bottom of a.txt
Split my window with <c-w>s
Refocus the top window to the top of a.txt and open b.txt for editing in the lower window
Use <c-6> to switch to alternate buffer repeatedly in the lower window
If I open a.txt for editing and the '" mark is positioned on line 1, this issue does not occur.
The first with this plugin installed:
When switching from b.txt to a.txt as the alternate buffer with this plugin installed, the cursor is returned to line 3, where my file was opened (the '" mark). This loss of context removes the benefit of having an alternate buffer for reference.
lastplace.mp4
Without the plugin installed:
no-lastplace.mp4
Without the plugin installed everything works as expected. I also tested without loading my .vimrc which also functioned properly. I am using Neovim 0.6 if that makes a difference.
This is different from the suggested command for vim because of differences in how neovim handles filetype loading: neovim/neovim#16339
I tried the above in neovim and I don't see the bug anymore.
vim-lastplace uses a BufWinEnter autocmd. Out of curiosity I replaced the BufWinEnter with BufRead in vim-lastplace and I cannot replicate this issue in either vim or neovim. Everything else seems to work okay still, though I did not test extensively. For example, I don't use folds and I didn't test if this impacted folds.
I'm not super familiar with the differences between BufWinEnter and BufRead, but that simple change seems to have resolved things.
@natecraddock thanks for posting a solution. To clarify, do you remove farmergreg/vim-lastplace and replace it with the code in your comment, or do you load this plugin in addition to the autocmd?
@nzbart Sorry for the ambiguity. I found two solutions:
Remove the plugin and use the above autocommand in neovim. This removes the bug, but this autocmd is missing a few features of lastplace (like centering the cursor). Vim has a similar autocmd documented at :h restore-cursor.
The fix for this bug may have created the conditions that created bug #30. keeping both bugs open until i have some time to dig into this. If anyone wants to look into it, please do so, and submit a patch when you figure it out!
Activity
farmergreg commentedon Dec 16, 2021
Hi Nathan, thanks for the truly excellent bug report.
I won't have availability to tackle this for quite some time.
If you or anyone else wants to work up a patch and submit it, I'd be more than happy to review it and pull it in to the mainline.
natecraddock commentedon Feb 16, 2022
I looked a bit more into this, and I can confirm that this bug also occurs in vim, not just neovim.
Neovim's docs suggest using the following for restoring the cursor position automatically:
This is different from the suggested command for vim because of differences in how neovim handles filetype loading: neovim/neovim#16339
I tried the above in neovim and I don't see the bug anymore.
vim-lastplace uses a
BufWinEnterautocmd. Out of curiosity I replaced theBufWinEnterwithBufReadin vim-lastplace and I cannot replicate this issue in either vim or neovim. Everything else seems to work okay still, though I did not test extensively. For example, I don't use folds and I didn't test if this impacted folds.I'm not super familiar with the differences between
BufWinEnterandBufRead, but that simple change seems to have resolved things.nzbart commentedon Feb 21, 2022
@natecraddock thanks for posting a solution. To clarify, do you remove farmergreg/vim-lastplace and replace it with the code in your comment, or do you load this plugin in addition to the autocmd?
natecraddock commentedon Feb 21, 2022
@nzbart Sorry for the ambiguity. I found two solutions:
Remove the plugin and use the above autocommand in neovim. This removes the bug, but this autocmd is missing a few features of lastplace (like centering the cursor). Vim has a similar autocmd documented at
:h restore-cursor.Make the following edit to lastplace
As I mentioned above, I'm not sure if solution 2 causes any side-effects, but it seems to work perfectly for me!
Fix window split position on Neovim
farmergreg commentedon Feb 22, 2022
Thank-you for the patch @natecraddock and @nzbart. Merged into mainline for testing.
Everyone, please test it out and leave comments here if you find something this patch breaks.
@oblitum would you be willing to re-test the fold feature and ensure it continues to work properly?
musjj commentedon Dec 3, 2022
Converted the autocmd from the docs to lua:
Not really that familiar with vimscript, so feel free to point out any mistakes.
farmergreg commentedon Aug 23, 2023
The fix for this bug may have created the conditions that created bug #30. keeping both bugs open until i have some time to dig into this. If anyone wants to look into it, please do so, and submit a patch when you figure it out!
Thanks!