Re: Avoid jumping cursor after undo
On 2022-03-14, Lifepillar wrote: > I have defined this function: > > fun! Lint() > let l:view = winsaveview() > keepjumps normal gggqG > call winrestview(l:view) > endf > > When the function is executed, the cursor does not move, as expected. > Unfortunately, pressing u after that makes the cursor jump to the top > of the buffer. I had hoped that keepjumps would prevent that, but that > does not seem the case. How can I avoid moving the cursor upon undoing > (without touching registers)? I have found a rational for this behaviour (Vim restores the position to where the undone change has happened), and a way to keep the cursor where it is: https://vim.fandom.com/wiki/Restore_the_cursor_position_after_undoing_text_change_made_by_a_script Perhaps, there is a less hackish way that does not modify the buffer. Life. -- -- You received this message from the "vim_use" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_use" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/t0tc0t%2410vm%241%40ciao.gmane.io.
Re: Avoid jumping cursor after undo
On Mon, Mar 14, 2022, 14:10 Lifepillar wrote: > I have defined this function: > > fun! Lint() > let l:view = winsaveview() > keepjumps normal gggqG > call winrestview(l:view) > endf > > When the function is executed, the cursor does not move, as expected. > Unfortunately, pressing u after that makes the cursor jump to the top of > the buffer. I had hoped that keepjumps would prevent that, but that does > not seem the case. How can I avoid moving the cursor upon undoing > (without touching registers)? > > Thanks, > Life. > I don't know of a built-in way, but maybe you could write a function as above that saves and restores the view, though it would be tricky if the undo adds or removes lines above the cursor location. Then, map u to it. It's easy to figure out whether the undo added or removed lines (and how many). Then, you could look at where the cursor has ended up. If it's before your original location, then you need to adjust the values you pass to winrestview to move the return location up or down by the number of rows added. It'll probably still be less than perfect if the change is right around the cursor location, but those don't move the cursor much. Salman > -- -- You received this message from the "vim_use" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_use" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEe_vn87LiVR%2Bf-rOPQsCJaYTnDcXEh7BhwHdrRDuE1cww%40mail.gmail.com.
Avoid jumping cursor after undo
I have defined this function: fun! Lint() let l:view = winsaveview() keepjumps normal gggqG call winrestview(l:view) endf When the function is executed, the cursor does not move, as expected. Unfortunately, pressing u after that makes the cursor jump to the top of the buffer. I had hoped that keepjumps would prevent that, but that does not seem the case. How can I avoid moving the cursor upon undoing (without touching registers)? Thanks, Life. -- -- You received this message from the "vim_use" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_use" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/t0o0f3%24i9e%241%40ciao.gmane.io.