Friday, January 20, 2023

Re: Why does (my) Vim leave the cursor to the last mark even if I don't save the file?



On Fri, Jan 20, 2023 at 11:01 AM Bram Moolenaar <Bram@moolenaar.net> wrote:

> I think what he is asking is for the " mark to only be written to the
> viminfo file for buffers that have been modified.  I'm not sure how to
> do this or if it is even possible, but it seems like a perfectly
> reasonable behavior to want.
>
> Perhaps a VimLeavePre autocommand that wipes unmodified buffers?  What
> other settings for those buffers would not be saved to .viminfo?

How about:
- When writing the buffer set some flag, e.g. b:did_write_this_buf
- When exiting, go through all buffers and those that don't have the
  b:did_write_this_buf flag move the cursor to the " mark.

Something like that.

It might also be better to go to the '. mark instead of leaving it where it ends up, if you DID make a change, so you're going back to the last place where a change was actually made rather than just where the cursor was when you closed the file, in case you moved around a bit after making a change.

This isn't what was asked, but I like to ignore the mark when opening certain files and go to a specific place in the file rather than where the cursor was, so I use this (legacy Vim script because I wrote it over a many years ago):

let g:noRestorePositionList = {}

let g:noRestorePositionList[ 'COMMIT_EDITMSG' ] = 'top'
let g:noRestorePositionList[ 'bugs\.vim' ]      = 'bottom'
let g:noRestorePositionList[ '.*\.money' ]      = 'ignore'

function! GoToLastEditedPosition()
  let filename = expand( "%:t" )

  let matchFound = 0
  let position   = ''

  for key in keys( g:noRestorePositionList )
    if ( filename =~? key )
      let matchFound = 1
      let position   = g:noRestorePositionList[ key ]
    endif
  endfor

  if ( matchFound )
    " Can't just execute 1 or $ because we don't just want to go to the line, but rather the beginning of the line
    if ( position == 'top' )
      normal! gg0
    elseif ( position == 'bottom' )
      normal! G0
    endif

    " Ignore empty value or if the value is 'ignore'.

    return
  endif

  let lastLine = line( "'\"" )

  if ( lastLine > 0 && lastLine <= line( '$' ) )
    " Go to the last cursor location and center it on the screen
    normal! g`"zz
  endif
endfunction

augroup lastedit
  " Go to the last edited position if it's valid and move it to the middle of the screen
  au BufReadPost * call GoToLastEditedPosition()
augroup END

--
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEdf1D99VT95tmiV377G%2BsdJ-_1VfLFjCybP1A8taNA7TQ%40mail.gmail.com.

No comments: