kickstart.nvim icon indicating copy to clipboard operation
kickstart.nvim copied to clipboard

Allow `zt` and `zb` to temporarily surpress `scrolloff` option

Open chantastic opened this issue 1 year ago • 4 comments

Describe the bug

scrolloff is set to 10 lines. This disallows zt and zb from hitting true top and bottom.

To Reproduce

In Normal, type the command zt or zb. Note that top and bottom are impacted by the default scrolloff value.

Desktop

  • OS: macOS
  • Terminal: Alacritty

Neovim Version

NVIM v0.10.1
Build type: Release
LuaJIT 2.1.1724232689

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.10.1/share/nvim"

Suggestion

Temporarily set scrolloff to zero, only for mapped commands (zt, zb).

-- Custom function to execute a command with temporary scrolloff=0
local function with_scrolloff_0(cmd)
  return function()
    local scrolloff = vim.o.scrolloff
    vim.o.scrolloff = 0
    vim.cmd('normal! ' .. cmd)
    vim.o.scrolloff = scrolloff
  end
end

-- Set up custom mappings for zt and zb
vim.keymap.set('n', 'zt', with_scrolloff_0 'zt', { noremap = true, silent = true })
vim.keymap.set('n', 'zb', with_scrolloff_0 'zb', { noremap = true, silent = true })

chantastic avatar Sep 27 '24 22:09 chantastic

dang. realizing this also only works momentarily. any new motions will add the scrolloff back.

interested in other possible solutions.

chantastic avatar Sep 27 '24 22:09 chantastic

Try just this.

vim.keymap.set('n', 'zt', ':let save_scrolloff = &scrolloff<CR>:set scrolloff=0<CR>zt:let &scrolloff = save_scrolloff<CR>')
vim.keymap.set('n', 'zb', ':let save_scrolloff = &scrolloff<CR>:set scrolloff=0<CR>zb:let &scrolloff = save_scrolloff<CR>')

rivenirvana avatar Sep 28 '24 03:09 rivenirvana

this is the same thing without a shared function, right?

chantastic avatar Sep 30 '24 17:09 chantastic

It works for me, at least. Can reach true top and bottom. Of course, any subsequent motions will factor in the scrolloff value once the keymap is finished. I'd suggest just removing the scrolloff value entirely if it impedes with your desired workflow, or have a separate keymap that toggles it on/off.

rivenirvana avatar Sep 30 '24 19:09 rivenirvana

No action items here? Looks like a work around has been offered. Closing. Please re-open if you feel there's actual work for us to do.

feoh avatar Oct 30 '24 19:10 feoh