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>AppDataLocalnviminit.lua:
E5112: Error while creating lua chunk: C:<user>AppDataLocalnviminit.lua:24: unexpected symbol near '/'
Press ENTER or type command to continue
My Questions
- Is this a syntax error?
- Is there any way to access
vim.opt.lines
or an equivalent value? - 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.
2
Upon further investigation, I have found the solution. The following code works as desired:
vim.opt.scrolloff = math.floor(vim.opt.lines:get() / 2)
- Yes. NeoVim uses Lua 5.1, whereas the
//
operator was only introduced in Lua 5.3. Therefore, this is a syntax error. - Yes. Use
vim.opts.lines:get()
. - You can access option values using the following syntax:
vim.opts.<option>:get()
. See:help vim.opt:get()
for more info.