0

(Vim 8.0, Windows 7)

I wanted to make an autocmd for loading a certain file, that scrolls down five lines from the beginning of the file. It does other things too, so the autocmd itself (inside of an augroup filetype, incidentally) is

au BufRead,BufNewFile foo.txt   so $VIM/foo.vim

where foo.vim contains various other commands, plus:

norm! 5^E

If I run the above norm command manually, it does exactly what I want. However, when I use the autocmd, the buffer ends up with the cursor on line 5, but with line 1 still visible. I get the same behavior if I replace ^E with ^D. Nothing also seems to happen if I follow either one with z^M.

I could find nothing in the documentation for au, so, ^E, or norm that addresses this. Is it a bug? Or am I missing something obvious?

  • 1
    Works fine for me. You sure you did Ctrl-V + Ctrl-E in the file with au? – B Layer Nov 21 '17 at 1:06
  • Very sure. I have syntax highlighting on, and the ^E is clearly a different color. Also, the cursor is going down that many spaces, so I know it's doing something with that command. – Paul Brinkley Nov 21 '17 at 9:02
1

Check whether you have a non-zero value for 'scrolloff'. This forces a minimum number of lines to be visible above/below the cursor. A value of 5 or more in this case would result in exactly the behavior you mention: cursor in the right place but top of the file still visible. A quick-and-diry test would be to just :set scrolloff=0 right before the au command.

If, indeed, 'scrolloff' appears to be non-zero but you are not setting it yourself then are you using a personal vimrc file? If not, note that Vim 8 has new behavior where it will source $VIMRUNTIME/defaults.vim automatically if user vimrc is not found and in that file 'scrolloff' is set to 5.

I have no idea if this is what's happening to you but it seems like a plausible theory. Plus, there's some generally useful info here, e.g. probably not a lot of people know about the new "defaults.vim" behavior.

  • Thanks for the input. Sadly, I'm sure neither of these is the culprit. First, if I run 5^E manually, even with a norm! command, it works, so the scrolloff would have to be set somehow in my vimrc afterward, and it's not (I have a vimrc, and it has no scroll settings, and sources nothing but one file with no scroll settings). Also, I have put in several different values - 2, 5, 10, 20 - for the scroll, and one of them should have worked no matter what the scrolloff was. // Secondly, this problem was happening under Vim 7.3 - I had just installed 8.0 since I saw a few scrolling bugs fixed. – Paul Brinkley Nov 21 '17 at 17:45
  • Hmm. Okay. Maybe you can try it with a baseline, all-defaults user vimrc. Just the following: unlet! skip_defaults_vim + source $VIMRUNTIME/defaults.vim + your au command. And add set scrolloff=0. That could tell us a thing or two depending on the result. – B Layer Nov 21 '17 at 20:48
  • That changed something. My vim window (which now looks very bare) scrolled properly. I left out the scrolloff setting, too. // Now trying to decide how to proceed. I could try the painstaking process of adding my original vimrc to this default, little by little. Hmm. – Paul Brinkley Nov 22 '17 at 19:43
  • ....and, done. Nothing in my _vimrc seems to have caused that scroll behavior, and now it's working correctly. But if I take out the unlet and the defaults, the behavior returns. So perhaps this is the answer. – Paul Brinkley Nov 22 '17 at 19:53
  • You say you "left out the scrolloff setting, too" and it still worked? That doesn't seem right. Here's what I see... First, I follow my steps exactly and result is correct and expected: line 5 at top of window. If then remove just set scrolloff=0 then it's in the "bad" state: cursor line 5 but line 1 at top of window. Have you tried with file other than .vim suffix? Try a file with .txt after modifying the au command appropriately. Could be default settings applied to .vim. – B Layer Nov 22 '17 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.