I would like to automatically set the scroll offset to half the value of vim.opt.lines
(see :help lines
).
I tried the following:
vim.opt.scrolloff = vim.opt.lines // 2
I expected this to retrieve the current value of vim.opt.lines
, divide it by 2, and assign the result to vim.opt.scrolloff
.
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
vim.opt.lines
or an equivalent value?init.lua
is being processed?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.
Upon further investigation, I have found the solution. The following code works as desired:
vim.opt.scrolloff = math.floor(vim.opt.lines:get() / 2)
//
operator was only introduced in Lua 5.3. Therefore, this is a syntax error.vim.opts.lines:get()
.vim.opts.<option>:get()
. See :help vim.opt:get()
for more info.