As title. I'm fixing a plugin and I need to know the scrolloff
set by user.
1 Answer
In vim, the options, as documented in :h option-list
, are stored in a set of variables. VimL variables have different scopes (see :h variable-scope
), but the variables that store the option values are named by prepending an ampersand to the option name: e.g., the variable for :h scrolloff
is &scrolloff
(see :h :let-&
). One can set the option (:set scrolloff
) through changing the value of this variable (:let &scrolloff = 0
). Although there exists two ways to set an option (with :set <option>
and :let &<option>
), there is only one good way to read the current state of an option programmatically: to access the associated variable.
The Lua API in neovim exposes these option-variables through :h lua-vim-options
. To access the default options provided by vim, one needs to access it through :h vim.o
or :h vim.opt
.
So for the OP question, the state of scrolloff
can be checked using the following method:
if vim.o.scrolloff >= 1 then
-- do something
end
-
While I have no experience with lua, I believe the comparison should be
vim.o.scrolloff >= 1
?– husBCommented Oct 6, 2022 at 14:30 -
1@husB it was supposed to be merely an example. OP had to swap
1
with their required value. But changed it to your suggestion anyway. It makes more sense, in that it executes the block if scrolloff is active (Natural number). Thanks for the suggestion.– 3N4NCommented Oct 6, 2022 at 14:40