简体   繁体   中英

How to add an empty space in the Neovim at the bottom of the buffer when using the `set scrolloff` option

scrolloff is a great feature, but it doesn't work on the last lines of the file. Is there a setting or plugin that allows you to add empty space at the bottom of the file (with '~' in the sign column). Like view, that gives if you scroll the file with the mouse wheel. Or 'zz' command.

I'm set scrolloff=10 . And it's uncomfortable to work if there is visually no free space at the bottom. And also I don't like when the autocomplete menu opens up and overlaps the previous text.

I've made an autocommand for that:

vim.api.nvim_create_autocmd({ "CursorMoved" }, {
  callback = function()

    -- normal buffer (not terminal or prompt)
    if vim.bo.buftype ~= "" then
      return
    end

    local windowLines = vim.api.nvim_win_get_height(0)
    local currLine = vim.fn.line(".")
    local lastLine = vim.fn.line("$")

    -- to handle a file smaller than window
    local bottom = 0
    if windowLines > lastLine then
      bottom = windowLines
    else
      bottom = lastLine
    end

    local marginBottom = currLine + vim.o.scrolloff - bottom
    if marginBottom == 0 then
      vim.api.nvim_input("zb")                    -- align cursor with bottom of file
    elseif marginBottom > 0 then
      vim.api.nvim_input("zb")                    -- align cursor with bottom of file
      vim.api.nvim_input(marginBottom .. "<C-E>") -- scroll down
    end
  end,
})

It's a little dirty but it works enough for me.
However, it only works in normal mode. If you want, you can extend the autocommand with the CursorMovedI event to work in insert mode (you'll have to make some changes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM