Smooth scrolling neovim plugin written in lua
| Files | Last commit | Last update |
|---|---|---|
| 1 year ago | ||
| 10 months ago | ||
| 4 years ago | ||
| 5 years ago | ||
| 1 year ago | ||
| 2 years ago | ||
| 2 years ago |
Neoscroll:一款用 Lua 编写的 neovim 平滑滚动插件
演示
功能特性
- 窗口移动命令的平滑滚动(映射可选):
<C-u>、<C-d>、<C-b>、<C-f>、<C-y>、<C-e>、zt、zz、zb。 - 支持折叠内容。
- 单一滚动函数,可接受行数或窗口百分比作为滚动参数。
- 可滚动任意窗口。
- 滚动时可选择隐藏光标,带来更舒适的滚动体验。
- 可自定义滚动行为。
- 可使用预定义的缓动函数实现滚动动画。
- 性能模式,滚动时关闭语法高亮,适用于性能较低的机器或包含大量正则语法高亮的文件。
- 通过向相反方向滚动可取消当前滚动。
- 按住按键滚动时模拟“释放按键即停止”功能。
- 可通过窗口 ID 滚动任意窗口
安装
使用本插件需要 neovim 0.5 或更高版本。通过你喜欢的插件管理器进行安装:
-
使用 Packer:
use 'karb94/neoscroll.nvim' -
使用 vim-plug:
Plug 'karb94/neoscroll.nvim' -
使用 lazy.nvim,创建文件
~/.config/nvim/lua/plugins/neoscroll.lua:return { "karb94/neoscroll.nvim", opts = {}, }
选项
查看 :help neoscroll-options 获取所有选项的详细说明。
包含所有选项及其默认值的 setup() 函数:
require('neoscroll').setup({
mappings = { -- Keys to be mapped to their corresponding default scrolling animation
'<C-u>', '<C-d>',
'<C-b>', '<C-f>',
'<C-y>', '<C-e>',
'zt', 'zz', 'zb',
},
hide_cursor = true, -- Hide cursor while scrolling
stop_eof = true, -- Stop at <EOF> when scrolling downwards
respect_scrolloff = false, -- Stop scrolling when the cursor reaches the scrolloff margin of the file
cursor_scrolls_alone = true, -- The cursor will keep on scrolling even if the window cannot scroll further
duration_multiplier = 1.0, -- Global duration multiplier
easing = 'linear', -- Default easing function
pre_hook = nil, -- Function to run before the scrolling animation starts
post_hook = nil, -- Function to run after the scrolling animation ends
performance_mode = false, -- Disable "Performance Mode" on all buffers.
ignored_events = { -- Events ignored while scrolling
'WinScrolled', 'CursorMoved'
},
})
你可以映射一组精简的默认快捷键:
require('neoscroll').setup({ mappings = {'<C-u>', '<C-d>', '<C-b>', '<C-f>'} })
或者你可以通过传入空列表来禁用所有默认映射:
require('neoscroll').setup({ mappings = {} })
以下部分将介绍如何创建自定义映射。
自定义映射
你可以使用以下 lua 函数创建自己的滚动映射:
scroll(lines, opts)ctrl_uctrl_dctrl_bctrl_fzt(opts)zz(opts)zb(opts)
有关更多详细信息,请参阅 :help neoscroll.scroll() 和 :help neoscroll-helper-functions。
你可以在 init.lua 中使用以下语法糖,在普通模式、可视模式和选择模式下定义 lua 函数映射:
neoscroll = require('neoscroll')
local keymap = {
["<C-u>"] = function() neoscroll.ctrl_u({ duration = 250 }) end;
["<C-d>"] = function() neoscroll.ctrl_d({ duration = 250 }) end;
["<C-b>"] = function() neoscroll.ctrl_b({ duration = 450 }) end;
["<C-f>"] = function() neoscroll.ctrl_f({ duration = 450 }) end;
["<C-y>"] = function() neoscroll.scroll(-0.1, { move_cursor=false; duration = 100 }) end;
["<C-e>"] = function() neoscroll.scroll(0.1, { move_cursor=false; duration = 100 }) end;
["zt"] = function() neoscroll.zt({ half_win_duration = 250 }) end;
["zz"] = function() neoscroll.zz({ half_win_duration = 250 }) end;
["zb"] = function() neoscroll.zb({ half_win_duration = 250 }) end;
}
local modes = { 'n', 'v', 'x' }
for key, func in pairs(keymap) do
vim.keymap.set(modes, key, func)
end
缓动函数
默认情况下,滚动动画采用恒定速度(线性),即每行滚动之间的时间间隔保持一致。若你希望平滑滚动动画的起始与结束阶段,可以向 scroll() 函数传入 Neoscroll 提供的任意缓动函数名称。目前支持的缓动函数包括:linear、quadratic、cubic、quartic、quintic、circular、sine。Neoscroll 会根据所选缓动函数动态调整每行滚动的时间间隔,这种动态调整能让动画视觉效果更舒适自然。
若想深入了解缓动函数,以下链接可供参考:
示例
借助“自定义映射”章节中介绍的语法糖,可编写如下配置:
neoscroll = require('neoscroll')
neoscroll.setup({
-- Default easing function used in any animation where
-- the `easing` argument has not been explicitly supplied
easing = "quadratic"
})
local keymap = {
-- Use the "sine" easing function
["<C-u>"] = function() neoscroll.ctrl_u({ duration = 250; easing = 'sine' }) end;
["<C-d>"] = function() neoscroll.ctrl_d({ duration = 250; easing = 'sine' }) end;
-- Use the "circular" easing function
["<C-b>"] = function() neoscroll.ctrl_b({ duration = 450; easing = 'circular' }) end;
["<C-f>"] = function() neoscroll.ctrl_f({ duration = 450; easing = 'circular' }) end;
-- When no value is passed the `easing` option supplied in `setup()` is used
["<C-y>"] = function() neoscroll.scroll(-0.1, { move_cursor=false; duration = 100 }) end;
["<C-e>"] = function() neoscroll.scroll(0.1, { move_cursor=false; duration = 100 }) end;
}
local modes = { 'n', 'v', 'x' }
for key, func in pairs(keymap) do
vim.keymap.set(modes, key, func)
end
pre_hook 和 post_hook 函数
设置 pre_hook 和 post_hook 函数可在滚动动画之前和/或之后运行自定义代码。该函数会接收 info 参数,此参数可选择性地传递给 scroll()(或任何提供的包装器)。这可用于针对不同类型的滚动动画有条件地运行不同的钩子。
例如,如果你只想为 <C-d>/<C-u> 滚动动画隐藏 cursorline,可以按如下方式操作:
require('neoscroll').setup({
pre_hook = function(info) if info == "cursorline" then vim.wo.cursorline = false end end,
post_hook = function(info) if info == "cursorline" then vim.wo.cursorline = true end end
})
local keymap = {
["<C-u>"] = function() neoscroll.ctrl_u({ duration = 250; info = 'cursorline' }) end;
["<C-d>"] = function() neoscroll.ctrl_d({ duration = 250; info = 'cursorline' }) end;
}
local modes = { 'n', 'v', 'x' }
for key, func in pairs(keymap) do
vim.keymap.set(modes, key, func)
end
请记住,info 变量并不仅限于字符串。它也可以是一个包含多个键值对的表。
已知问题
<C-u>、<C-d>、<C-b>、<C-f>会干扰宏的正常工作 (问题)。
致谢
本插件的灵感来源于 vim-smoothie 和 neo-smooth-scroll.nvim。 非常感谢它们的作者!