A simple autocmd that keeps your cursor at the center of the screen in all contexts.
No jerkiness when changing modes or jumping to different LOCs.
Replacement for what you might typically do in keybindings such as:
nnoremap "j" "jzz"
nnoremap "n" "nzzzv"
" ...
" etcUsing autocmd and CursorMoved/CursorMovedI events, zz is applied to every keystroke that would change the cursor position.
Minorly optimized by only applying zz to vertical line movement.
Should not get in the way of plugins like auto-pairs or nvim-cmp, which tend to have their own mappings for <CR>.
Using Lazy:
{
'arnamak/stay-centered.nvim'
}Using Packer:
use 'arnamak/stay-centered.nvim'require('stay-centered').setup({
-- The filetype is determined by the vim filetype, not the file extension. In order to get the filetype, open a file and run the command:
-- :lua print(vim.bo.filetype)
skip_filetypes = {},
-- Set to false to disable by default
enabled = true,
-- allows scrolling to move the cursor without centering, default recommended
allow_scroll_move = true,
-- temporarily disables plugin on left-mouse down, allows natural mouse selection
-- try disabling if plugin causes lag, function uses vim.on_key
disable_on_mouse = true,
})In Lazy:
{
'arnamak/stay-centered.nvim',
lazy = false,
opts = {
skip_filetypes = { 'lua', 'typescript' },
}
}In Packer
use {
'arnamak/stay-centered.nvim',
config = function()
require('stay-centered').setup({
skip_filetypes = { 'lua', 'typescript' }
})
end
}stay-centered.nvim has built-in functions enable, disable, and toggle to handle this behavior.
Example for toggling on keymap:
vim.keymap.set({ 'n', 'v' }, '<leader>st', require('stay-centered').toggle, { desc = 'Toggle stay-centered.nvim' })Setting scrolloff=1000 will achieve similar effects, albeit with a few key differences:
- Scrolling (e.g. mouse,
<C-e>,<C-y>, etc.) will always move the cursor - Padding on the bottom of the screen will not be added
- Using this large of a scroll off prevents commands like
zt,zb,H,Lfrom doing anything