As title. Since no one wants to become a turtle, I want to setup a behavior like M but instead pin the current line to 80% height (0% start from bottom) of the current window, not to center.

  • 1
    M doesn’t center lines; it goes to the middle line. Do you perhaps mean zz, zt, etc.?
    – D. Ben Knoble
    May 27, 2022 at 14:20

2 Answers 2


:h H accepts a count, so you can do something like this:

exe "normal " .. winheight(0) * 80 / 100 .. "H"

which translates to XH where X is a number.



Since I don't like scrolloff, I update to this new version:

vim.api.nvim_set_keymap('n', 'M', '<cmd>lua pin_to_80_percent_height()<CR>', NOREF_NOERR_TRUNC)
function _G.pin_to_80_percent_height()
  vim.opt.scrolloff = 5
  vim.cmd("exe 'normal! zt'")
  vim.opt.scrolloff = 0

old answer

  1. Set scrolloff to 5

    vim.opt.scrolloff = 5
  2. Set keymap M to zt.

    vim.api.nvim_set_keymap('n', 'M', 'zt', NOREF_NOERR_TRUNC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.