I have to navigate through a large code base and it seems the thing that best suits me is the setting for scroll offset

:set scrolloff=50

However if I have to click on the random spot on the screen or move to a random spot on the screen using / scrolloff=50 doesn't help.

Is there a way for me to tell vim that set scrolloff=50 only when I press <C-d> or <C-u>? After every <C-d> or <C-u> return scroll offset to 0 so it will be scrolloff=0

1 Answer 1


Yes, by simple making a mapping:

nnoremap <C-d> :set scrolloff=50<CR><C-d>:set scrolloff=0<CR>

If I understand your intention correct, then centring the screen with zz may actually be more appropriate though:

nnoremap <C-d> <C-d>zz

Also see: How do I center the current line in the screen?.

I recommend you read :help key-mapping and/or the appropriate chapters of Learn Vimscript the hard way, by the way.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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