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

Feature: Option to center the cursor (zz) if below center #70

Open
keiviv opened this issue Jul 5, 2024 · 3 comments
Open

Feature: Option to center the cursor (zz) if below center #70

keiviv opened this issue Jul 5, 2024 · 3 comments

Comments

@keiviv
Copy link

keiviv commented Jul 5, 2024

Amazing plugin, and an option to auto-zz if below center would be useful. I've tried to add it in custom_actions, but it needs to be after the return, and also work on previous • next. The code is quite simple to implement:

if vim.fn.line('.') > vim.fn.winheight(0) / 2 then
   vim.cmd('normal! zz')
end

or similar. The option name could be smart_center.

@tingey21
Copy link
Contributor

I'll take a look at this in a little 👀 I do love me some auto centering

@xzbdmw
Copy link
Contributor

xzbdmw commented Jul 21, 2024

Can you try

            open = function(target_file_name, current_file_name)
                vim.api.nvim_create_autocmd("BufEnter", {
                    once = true,
                    callback = function()
                        vim.api.nvim_create_autocmd("CursorMoved", {
                            once = true,
                            callback = function()
                                if vim.fn.line(".") > vim.fn.winheight(0) / 2 then
                                    vim.cmd("normal! zz")
                                end
                            end,
                        })
                    end,
                })
                vim.cmd("e " .. target_file_name)
            end, -- target_file_name = file selected to be open, current_file_name = filename from where this was called

@keiviv
Copy link
Author

keiviv commented Aug 16, 2024

Can't test due to health issues, but it does look like autocmd would work. But I do think it'd be cooler to implement at the plugin level — as it does not add much code or overhead, and is a practically useful option — leaving a cursor at the end is quite common, and an option to center it vertically is nice.

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

3 participants