nvim-lua / kickstart.nvim

A launch point for your personal nvim configuration
MIT License
18.93k stars 21.78k forks source link

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

Open chantastic opened 1 week ago

chantastic commented 1 week ago

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

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 commented 1 week ago

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

interested in other possible solutions.

rivenirvana commented 1 week ago

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>')
chantastic commented 6 days ago

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

rivenirvana commented 6 days ago

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.