2

I want to scroll the current line to be the topmost line in the window, even with scroloff set.

How do I do this?

I want to use it with:

com! TestColours :echo "Please wait..." | :so /usr/share/vim/vim74/syntax/colortest.vim | normal 8G{{move line to top of screen}}
  • 1
    Well, the whole point of scrolloff is precisely to prevent zt and other commands to reach the top and bottom of the window so… you'll have to unset it. – romainl Aug 12 '16 at 6:09
  • @romainl It's reasonable to want zt to ignore scrolloff while all the "other commands" respect it. – Rich Aug 12 '16 at 9:42
  • @Rich It's even more reasonable to fix the "other commands" to respect scrolloff. – Sato Katsura Aug 12 '16 at 9:44
  • @SatoKatsura That's the default behaviour. – Rich Aug 12 '16 at 9:51
  • Edited question to remove reference to zt. – Tom Hale Aug 12 '16 at 10:01
2

If the goal is simply to place line 8 at the top of the window, you can achieve this in the command line with:

:execute "normal gg7\<c-e>"

This jumps to the top of the file, and then scrolls down 7 lines.

Update

Further to discussions in the comments, in order to create a zT mapping that moves the current line to the top of the window without using the <c-e> normal mode command, you can do this:

:nnoremap zT :execute "normal! " . (line(".") + &scrolloff) . "Gzt"<cr>

Old answer

The below was written when the question implied the user wanted to change the behaviour of 'zt' Not removing it, because it includes some useful features of Vimscript.

You could achieve this by remapping 'zt' to scroll the extra amount by using the normal mode <c-e> command with a count of &scrolloff after carrying out its usual behaviour:

:nnoremap zt zt:execute "normal!" . &scrolloff . "\<lt>c-e>\<lt>esc>"<cr>

Alternatively, if your goal is simply to move line 8 to the top of the window, and you don't want to change the behaviour of 'zt', you could instead change your command to:

... | execute "normal! " . (8 + &scrolloff) . "Gzt"

This places the cursor &scrolloff lines below line 8, and then performs a standard zt, moving line 8 to the top of the window.

Note that after using either solution, the line at the very top of the screen will no longer be the "current" line. This is impossible to achieve with 'scrolloff' set, as that state is precisely what 'scrolloff' is intended to prevent.

  • Both zT and zB are unmapped by default, so I used: nnoremap zT zt:execute "normal!" . &scrolloff . "\<lt>C-e>\<lt>ESC>"<CR> and nnoremap zB zb:execute "normal!" . &scrolloff . "\<lt>C-y>\<lt>ESC>"<CR> – Tom Hale Aug 12 '16 at 10:17
  • I used the second form in the particular !com as per the question -- thanks! – Tom Hale Aug 12 '16 at 10:20
  • @TomHale See my newest, much simpler answer if you don't actually need your new zT mapping for anything else. – Rich Aug 12 '16 at 10:23
  • How would you get the current line number in vim? Something like &currentline? – Tom Hale Aug 12 '16 at 10:25
  • For finding the current line in Vimscript, you want, line("."). &option gives you the value of a Vim option (the things you set with :set). – Rich Aug 12 '16 at 10:35

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.