0

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.
    – statox
    Aug 11 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.
    – a20
    Aug 11 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.
    – statox
    Aug 11 at 12:05
  • 1
    Welcome to Vi and Vim!
    – D. Ben Knoble
    Aug 11 at 16:40
2

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.
    – a20
    Aug 11 at 12:27

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.