3

How can I make scroll lock work in Vim (windowed and terminal versions)?

When scroll lock is activated, using the arrow keys on the keyboard should scroll the window instead of moving the pointer.

When scroll lock is deactivated, the pointer should be moving as normally.

4
  • I think you are looking for ctrl+e and ctrl+y :h CTRL-E :h CTRL-Y so I'd say no need for a scroll lock button. Commented Aug 11, 2021 at 11:57
  • Indeed that does the same thing, but it is a lot more convenient (at least for me) to activate the scroll lock and then simply use the arrow keys. This is how scroll lock traditionally works so I am surprised it is not implemented in vim oob. Commented Aug 11, 2021 at 12:02
  • A lot of things don't work the traditional way in Vim and often that's what makes its strength. However you could implement the mechanism yourself, see my answer. Commented Aug 11, 2021 at 12:05
  • 1
    Welcome to Vi and Vim! Commented Aug 11, 2021 at 16:40

2 Answers 2

6

I think the best way to do what you want is to use ctrle to scroll up and ctrly to scroll down as the doc says:

However if you really want to use the arrow keys and have a scroll lock mechanism you could implement it yourself by adding this to your vimrc:

" This variable will hold the state of the scroll lock " 0: disabled 1: endabled let g:scrollLock = 0 " This command toggles the state of the scroll lock command! ToggleScrollLock let g:scrollLock = !g:scrollLock " If you want to avoid typing the previous command each time you can use a mapping like this one (Replace <F1> by your preferred key) nnoremap <silent> <F1> :ToggleScrollLock<CR> " And these are conditional mappings depending on the value of g:scrollLock the arrow keys will behave differently nnoremap <expr> <Up> (g:scrollLock == 1) ? '<C-y>' : '<up>' nnoremap <expr> <Down> (g:scrollLock == 1) ? '<C-e>' : '<down>' 

See :h :map-<expr> for more details about the last mappings.

Also I would encourage you to read :h scrolling there is a lot of different ways to scroll through a buffer in Vim, knowing all of your options might help you understanding why Vim doesn't have a traditional scroll lock mechanism.

1
  • 1
    Thank you! I was unable to find a way of mapping to ScrollLock, so I used the PauseBreak button instead (key value <F21>), and it works great. Commented Aug 11, 2021 at 12:27
1

Use the scrolloff setting and set it to a value larger than half the window height I think you'll get the behavior you really want.

:set scrolloff=999 

In this mode both normal navigation keys (j, k) as well as the arrow keys will cause the text to scroll while the cursor line stays in the middle of the view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.