terminalluaneovimscroll-offset

Can I automatically set the scroll offset to half of the terminal height?


The Problem

I would like to automatically set the scroll offset to half the value of vim.opt.lines (see :help lines).

I Tried

I tried the following:

vim.opt.scrolloff = vim.opt.lines // 2

I Expected

I expected this to retrieve the current value of vim.opt.lines, divide it by 2, and assign the result to vim.opt.scrolloff.

Results

This is what happened instead (upon reopening NeoVim):

Error detected while processing C:\Users\<user>\AppData\Local\nvim\init.lua:
E5112: Error while creating lua chunk: C:\<user>\AppData\Local\nvim\init.lua:24: unexpected symbol near '/'
Press ENTER or type command to continue

My Questions

  1. Is this a syntax error?
  2. Is there any way to access vim.opt.lines or an equivalent value?
  3. Can I not access vim option values? Is this universally true, or only true in this situation because vim options are still being defined while init.lua is being processed?

Note

I have previously just used

vim.opt.scrolloff = 26

which worked fine, but I was experimenting and wondering whether I could set it dynamically, which would be useful if I ever want to set the scroll offset to a quarter of the screen or something.


Solution

  • Upon further investigation, I have found the solution. The following code works as desired:

    vim.opt.scrolloff = math.floor(vim.opt.lines:get() / 2)
    
    1. Yes. NeoVim uses Lua 5.1, whereas the // operator was only introduced in Lua 5.3. Therefore, this is a syntax error.
    2. Yes. Use vim.opts.lines:get().
    3. You can access option values using the following syntax: vim.opts.<option>:get(). See :help vim.opt:get() for more info.