How do I open a link in Google chrome in Lua for neovim?

Viewed 322

I want to open links in chrome by gx or gf (to unify with how I open files currently)

How it could be done for neovim (lua config)?

1 Answers

Figured out a solution (with the help of https://stackoverflow.com/a/68694743/1427399, to make correct regexp)

M = {}
M.HandleURL = function()
  local url = string.match(vim.fn.getline("."), "[a-z]*://[^ >,;]*")
  if url ~= "" then
    vim.cmd('exec "!open \'' .. url .. '\'"')
  else
    vim.cmd('echo "No URI found in line."')
  end
end

vim.api.nvim_set_keymap("n", "gf", [[ <Cmd>lua M.HandleURL()<CR> ]], {})
Related