Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zt and zb to temporarily surpress scrolloff option #1165

Open
chantastic opened this issue Sep 27, 2024 · 4 comments
Open

Allow zt and zb to temporarily surpress scrolloff option #1165

chantastic opened this issue Sep 27, 2024 · 4 comments

Comments

@chantastic
Copy link

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
Copy link
Author

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

interested in other possible solutions.

@rivenirvana
Copy link
Contributor

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
Copy link
Author

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

@rivenirvana
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants