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"
" ...
" etc
Using 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,
})
In Lazy:
{
'arnamak/stay-centered.nvim'
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 behaviour.
Example for toggling on keymap:
vim.keymap.set({ 'n', 'v' }, '<leader>st', require('stay-centered').toggle, { desc = 'Toggle stay-centered.nvim' })