Sunday, June 3, 2018

Re: First line starting with space affects cursor position when switching buffers

On Mon, Jun 4, 2018 at 2:50 AM, dmccooey@comcast.net
<dmccooey@comcast.net> wrote:
> On Saturday, 2 June 2018 02:29:42 UTC-5, Bram Moolenaar wrote:
>> Andy Wokula wrote:
>>
>> > Am 01.06.2018 um 08:25 schrieb dmccooey@comcast.net:
>> > > Hello,
>> > >
>> > > I have encountered what appears to be a bug in vim,
>> > > but it might be a feature that I would like to disable.
>> > >
>> > > To reproduce the problem:
>> > >
>> > > Use vim to create a new file, and add 10 or so lines of text to the file.
>> > > In the first line, put at least two characters, and make sure the first character is a space or tab.
>> > > Place the cursor on the last line (or any line other than the first line).
>> > > Save the file (:w) and switch (:e) to view another file.
>> > > Switch back (:e) to the original file.
>> > > Notice that the cursor is on the first line, not where it was originally.
>> > >
>> > > I am using vim 8.0 for Mac.
>> > >
>> > > Dave McCooey
>> >
>> > I've seen this too for a long time (before v8.0), but didn't bother to investigate ...
>> > It's easy to "fix": just don't start the first line with whitespace.
>> >
>> > Ok, I did some bisecting (first time ^^):
>> >
>> > gvim-v7-4-674.exe (and earlier) works ok
>> > gvim-v7-4-675.exe (and later) shows the bug
>> >
>> > Someone should take a look at Patch 7.4.675
>>
>> That patch looks fine.
>>
>> Vim normally puts the cursor in the first line. There must be an
>> autocommand that is moving it to the last used position, the problem is
>> most likely in that autocommand.
>>
>> You can use $VIMRUNTIME/macros/editexisting.vim
>>
>> --
>> hundred-and-one symptoms of being an internet addict:
>> 16. You step out of your room and realize that your parents have moved and
>> you don't have a clue when it happened.
>>
>> /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
>> /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
>> \\\ an exciting new programming language -- http://www.Zimbu.org ///
>> \\\ help me help AIDS victims -- http://ICCF-Holland.org ///
>
> Hello Bram,
>
> When switching back and forth between two files using :e#,
> and assuming there are no autocommands involved, does vim
> normally put the cursor in the first line after each switch?
>
> I ask because I don't have autocommands involved, and I don't
> see vim behaving this way.
>
> There may be some confusion in this thread, because Christoph's
> reply referred to the case where a file is being opened for the
> first time. My post is not about that case. When I open a
> file for the first time, I expect vim to put the cursor in the
> first line (and it does).
>
> Dave

When there are no autocommands involved, or when a file with no marks
remembered is opened, the cursor goes to the first line. I just
checked where in the first line, and it is on the first nonblank.

However there has been for some time an autocommand in
vimrc_example.vim, and now in defaults.vim, which sets the cursor at
the last known position, if there is one. That is the autocommand
mentioned by Chr. von Stuckrad, and nowadays the defaults.vim defines
it in the group vimStartup. I remember a time when that autocommand
didn't exist by default, though, so it must have been added to the
vimrc_example.vim (and IIRC in group vimrcEx) at some point in Vim 6
or 7. I just tried to put the cursor on the first character of the
first line, a space, but even after doing ":wv" prior to quitting the
file, it is reopened with the cursor on the first nonblank even though
the " mark is at column zero. But if the cursor is on a different line
than the first when quitting, then if it was in column zero, even on a
space, the file will be reopened with the cursor where it used to be.

This baffles me, because the autocommand in the defaults.vim reads:

" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid, when inside an event handler
" (happens when dropping a file on gvim) and for a commit message (it's
" likely a different one than last time).
autocmd BufReadPost *
\ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
\ | exe "normal! g`\""
\ | endif

Now I named the file "xx.txt" (the filetype doesn't include "commit")
and when the cursor was on line 1, and the " mark is remembered as
"line 1 column zero" then line("'\"") is 1, which is >= 1 and <=
line("$"), isn't it? Yet this autocommand puts the cursor on the first
nonblank in column 1 in that case, but doing g`" manually, or even
doing :exe ":normal g'\"" manually, puts it in the first column, even
if that character is a space.


Best regards,
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment