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

Add support for `gg`/`G`

Open ouuan opened this issue 3 years ago • 9 comments

I added these in my config:

t['gg']    = {'scroll', {'1 - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}
t['G']     = {'scroll', {'vim.api.nvim_buf_line_count(0) - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}

And it turns out that it doesn't work well when there are wrapped lines, because neoscroll uses gj and gk, which is good for other commands like <c-u>/<c-d> but bad for gg and G.

Or is there any way to get the display line number? I didn't find it in help or online.

ouuan avatar May 09 '21 02:05 ouuan

Yes gg and G have some complications. The good news is that it is relatively easy to implement inside Neoscroll so the best solution is for me to make wrapper functions like I did for zt/zz/zb.

karb94 avatar May 09 '21 10:05 karb94

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}

karb94 avatar May 12 '21 09:05 karb94

Similarly, it would be good if pageup and pagedown were captured by neoscroll.

vext01 avatar Oct 07 '21 14:10 vext01

I finally had some time to look at this. I'm not entirely sure what would be the expected behaviour when an easing function is enabled. There are two parts for this animation:

  1. The window scrolls till the edge of the buffer
  2. The cursor scrolls till the edge of the buffer

The cursor has to scroll after the window reaches the edge of the buffer because the cursor cannot be at the bottom/top of the window if scrolloff is greater than 0.

Having said that, when an easing function is enabled do we want the easing function to apply to both movements, only on the window scroll movement or only on the cursor movement?

karb94 avatar Oct 09 '21 19:10 karb94

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

maxwelljens avatar Feb 03 '22 12:02 maxwelljens

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

veyxov avatar May 10 '22 11:05 veyxov

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

I just duplicated mapping for PageUp and PageDown and it seems to work nicely:

t['<C-b>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageUp>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<C-f>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageDown>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}

lawrence-laz avatar Aug 02 '22 20:08 lawrence-laz

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}

This is great, thanks. The only issue with this for me is that if the file is shorter than the height of the window, then the file will scroll to the last line leaving only the last line of the file visible at the top of the window.

My workaround for this: (only smooth scroll if buffer is longer than window height)

local api = vim.api

vim.keymap.set('n', 'G', [[<Cmd> lua maybe_smooth_scroll()<CR>]], { noremap = true })

function _G.maybe_smooth_scroll()
  local win_height = api.nvim_win_get_height(0)
  local buf_line_count = api.nvim_buf_line_count(0)

  if buf_line_count < win_height then
    api.nvim_command('normal! G')
  else
    require('neoscroll').scroll(1 * buf_line_count, true, 1, 5)
  end
end

benwoodward avatar May 03 '23 01:05 benwoodward