0

As title. I'm fixing a plugin and I need to know the scrolloff set by user.

1
  • 1
    @kadekai I think not everyone knows about this, and thus your words deserve points and should be put into an answer. Commented Oct 5, 2022 at 10:12

1 Answer 1

2

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
2
  • While I have no experience with lua, I believe the comparison should be vim.o.scrolloff >= 1?
    – husB
    Commented 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.
    – 3N4N
    Commented Oct 6, 2022 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.