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

22 views
Skip to first unread message

Ottavio Caruso

unread,
Jan 19, 2023, 10:00:35 AM1/19/23
to vim...@googlegroups.com
Ok, the title is probably not accurate, so I'll try to explain.


Let's say I have a file with 5 lines

--------------------------

This is line 1
Another line
More lines
Even more lines
This is the last line

-------------------------

I save the file (Esc w q). When I reopen it, the cursor is at the end of
line 5.

Then I move the cursor up two lines. I quit without saving (Esc q!).

When I reopen the file, the cursor is now at the 3rd line.

Is this intended behaviour? If so, how can I tell Vim to ignore
movements but just this time?

$ cat .vim/vimrc
source $VIMRUNTIME/defaults.vim

"set shellcmdflag=-ic
set dir=~/.vim/tmp
set expandtab
set autoindent
set mouse=r

digraph bl 8226 " Insert Bullet with <CTRL>+k bl


iabbrev cwd <C-R>=strftime("%a %d/%m %H:%M")
iabbrev mydate <C-R>=strftime("%a %d/%m/%Y")
iabbrev mydate2 <C-R>=strftime("%Y%m%d")

map <f5> :1m$<cr>








--
Ottavio Caruso

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

Christian Brabandt

unread,
Jan 19, 2023, 11:40:53 AM1/19/23
to vim...@googlegroups.com

On Do, 19 Jan 2023, 'Ottavio Caruso' via vim_use wrote:

> Ok, the title is probably not accurate, so I'll try to explain.
>
>
> Let's say I have a file with 5 lines
>
> --------------------------
>
> This is line 1
> Another line
> More lines
> Even more lines
> This is the last line
>
> -------------------------
>
> I save the file (Esc w q). When I reopen it, the cursor is at the end of
> line 5.
>
> Then I move the cursor up two lines. I quit without saving (Esc q!).
>
> When I reopen the file, the cursor is now at the 3rd line.
>
> Is this intended behaviour? If so, how can I tell Vim to ignore movements
> but just this time?
>

The behaviour you are seeing, comes from a BufReadPost autocommand, that
restores the last cursor position. It's defined in the help below
:h last-position-jump

> $ cat .vim/vimrc
> source $VIMRUNTIME/defaults.vim

This is where the auto command is defined. If you do not want it, you
can delete this after sourcing the defauls.vim file:

:augroup vimStartup | au! | augroup END


Best
Christian
--
Es mag zu meinem Vorteil oder Nachteil ausfallen, ich fürchte nicht,
so gesehen zu werden, wie ich bin.
-- Jean Jacques Rousseau (an Malesherbes, 1762)

Tony Mechelynck

unread,
Jan 19, 2023, 5:22:35 PM1/19/23
to vim...@googlegroups.com
Yes, indeed, you can suppress it if you want: everything (or almost)
is configurable in Vim (and I just checked: it is the only autocommand
in group vimStartup so no side-effects). Or else, you can decide that
it is a feature, not a bug, and enjoy having all your editfiles
reopened just where you were the last time you used them and not
always at the very top.

Best regards,
Tony.
>
>
> Best
> Christian
> --
> Es mag zu meinem Vorteil oder Nachteil ausfallen, ich fürchte nicht,
> so gesehen zu werden, wie ich bin.
> -- Jean Jacques Rousseau (an Malesherbes, 1762)
>
@Christian: weißt Du wie Jean-Jacques Rousseau dies auf Französisch schreibte?

Christian Brabandt

unread,
Jan 20, 2023, 2:38:36 AM1/20/23
to vim...@googlegroups.com

On Do, 19 Jan 2023, Tony Mechelynck wrote:

> > Es mag zu meinem Vorteil oder Nachteil ausfallen, ich fürchte nicht,
> > so gesehen zu werden, wie ich bin.
> > -- Jean Jacques Rousseau (an Malesherbes, 1762)
> >
> @Christian: weißt Du wie Jean-Jacques Rousseau dies auf Französisch schreibte?

Unfortunately Not. And my French is very bad, had it in school for 5
years and I do not remember anything, just enough for:
Je ne parle pas francais :)

Best,
Chris

--
Am Abend wird man klug
Für den vergangenen Tag,
Doch niemals klug genug
Für den, der kommen mag.
-- Friedrich Rückert (Gedichte, Pseudonym: Freimund Raimar)

Manfred Lotz

unread,
Jan 20, 2023, 4:30:17 AM1/20/23
to vim...@googlegroups.com


On 1/20/23 08:38, Christian Brabandt wrote:
>
> On Do, 19 Jan 2023, Tony Mechelynck wrote:
>
>>> Es mag zu meinem Vorteil oder Nachteil ausfallen, ich fürchte nicht,
>>> so gesehen zu werden, wie ich bin.
>>> -- Jean Jacques Rousseau (an Malesherbes, 1762)
>>>
>> @Christian: weißt Du wie Jean-Jacques Rousseau dies auf Französisch schreibte?
>
> Unfortunately Not. And my French is very bad, had it in school for 5
> years and I do not remember anything, just enough for:
> Je ne parle pas francais :)
>

I asked ChatGPT and it gave

"Ce qu'on pense de moi, et qui puisse tourner à mon avantage ou à mon
désavantage, ne me fait point d'appréhension; je suis ce que je suis." –

Jean-Jacques Rousseau, à Malesherbes, 1762.


As link it had:

https://www.goodreads.com/quotes/933781-ce-qu-on-pense-de-moi-et-qui-puissetourner-à-mon

but the link doesn't work and ChatGPT wasn't able to provide a working link.


--
Manfred

Ottavio Caruso

unread,
Jan 20, 2023, 5:29:27 AM1/20/23
to vim...@googlegroups.com
Thanks, but if do that, I'd then remove this feature from all files,
whether edited or not, wouldn't I?. Is it possible to have this
implemented only on files that are saved?

Christian Brabandt

unread,
Jan 20, 2023, 5:39:56 AM1/20/23
to vim...@googlegroups.com

On Fr, 20 Jan 2023, 'Ottavio Caruso' via vim_use wrote:

> Am 19/01/2023 um 16:40 schrieb Christian Brabandt:
> > On Do, 19 Jan 2023, 'Ottavio Caruso' via vim_use wrote:
> > > $ cat .vim/vimrc
> > > source $VIMRUNTIME/defaults.vim
> > This is where the auto command is defined. If you do not want it, you
> > can delete this after sourcing the defauls.vim file:
> >
> > :augroup vimStartup | au! | augroup END
>
> Thanks, but if do that, I'd then remove this feature from all files, whether
> edited or not, wouldn't I?. Is it possible to have this implemented only on
> files that are saved?

I would find it inconsistent for somtimes jumping to a different spot in
a file and sometimes not. I might not even remember if I last time saved
that particular file or not (e.g. I just need to check something but did
not save the file). That might be confusing.

It might be possible to change that behaviour using a custom function
(e.g. set a flag on BufWritePost, check that flag on a BufLeave
autocomamnd and set the '" mark accordingly). Not sure if this will work
or not.

Mit freundlichen Grüßen
Christian
--
Herr, du hast mir das Können genommen, so nimm mir auch noch das Wollen.

Marvin Renich

unread,
Jan 20, 2023, 8:32:53 AM1/20/23
to vim...@googlegroups.com
* Christian Brabandt <cbl...@256bit.org> [230120 05:39]:
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?

...Marvin

Bram Moolenaar

unread,
Jan 20, 2023, 11:01:35 AM1/20/23
to vim...@googlegroups.com, Marvin Renich

> 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.

--
What a wonderfully exciting cough! Do you mind if I join you?
-- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Tony Mechelynck

unread,
Jan 20, 2023, 11:09:28 AM1/20/23
to Manfred Lotz, vim...@googlegroups.com
Ah, thanks Manfred.
Tony.
> --
> --
> 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+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/95769a3f-415d-b5c2-1f2d-1e925b02d4e4%40posteo.de.

Salman Halim

unread,
Jan 20, 2023, 11:29:52 AM1/20/23
to vim...@googlegroups.com
On Fri, Jan 20, 2023 at 11:01 AM Bram Moolenaar <Br...@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

Marvin Renich

unread,
Jan 21, 2023, 10:53:53 AM1/21/23
to vim...@googlegroups.com
* Bram Moolenaar <Br...@moolenaar.net> [230120 11:01]:
>
> > 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.

I don't think we need another flag; just use the 'modified' option. I
think this is what the OP wants, and is simpler.

I also think Salman Halim's suggestion of using the . mark instead of the
" mark is more useful.

...Marvin

Marvin Renich

unread,
Jan 21, 2023, 11:11:28 AM1/21/23
to vim...@googlegroups.com
* Marvin Renich <mr...@renich.org> [230121 10:53]:
Note that what the OP wants (don't update " mark for non-modified
buffers) requires changing the code that writes .viminfo, which I think
is in the source, not the runtime.

If you change defaults.vim to use the . mark instead, the OP is going to
get very close to the behavior he wants (he may even like it better than
what he asked for), and no change to the Vim source is needed.

I suspect that most Vim users will prefer the . mark behavior over the
current behavior, but if not, they can easily get the old behavior back;
just leave the old BufReadPost as a comment in defaults.vim, and they
can copy it to their own vimrc.

I think this is the best solution all around.

...Marvin

Cesar Romani

unread,
Feb 1, 2023, 10:23:23 AM2/1/23
to vim...@googlegroups.com
Hi Tony,
I'm sending you a link, where probably there's the original French from
the quote:
https://athena.unige.ch/athena/rousseau/confessions/rousseau-confessions.html
Look for the paragraph which begins with "Je crois que voilà de quoi faire".

--
Cesar

Reply all
Reply to author
Forward
0 new messages