4

How do I translate this VimL code into Lua?

if executable('rg')
  set grepprg=rg\ --vimgrep
endif
1
  • 2
    Really, you want to translate it into Lua uses NeoVim's Lua APIs; for that, you probably just want to go through their relevant :help pages to learn how to set options or test executable().
    – D. Ben Knoble
    Jul 25, 2022 at 15:58

1 Answer 1

6

The answer below assumes you are running neovim and not vim.

:h vim.cmd() lets you execute a vimscript command from lua. It takes the command as a string. So you can wrap any vimscript in a string and pass it to :lua vim.cmd() and it should work.

vim.cmd[[
if executable('rg')
  set grepprg=rg\ --vimgrep
endif
]]

On the other hand, if you want a more lua-like approach, you can use :h vim.fn to call any vim function, both user-defined and built-in. With :h vim.o or :h vim.opt you can set the vim options.

if vim.fn.executable('rg') == 1 then
  vim.o.grepprg = 'rg --vimgrep'
end
1
  • I understand your post suggest two different ways. Maybe could you make it more explicite. Jul 28, 2022 at 9:38

Your Answer

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

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