2

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)?

Sergey Lapin
  • 1,008
  • 3
  • 12
  • 21

2 Answers2

1

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> ]], {})
Sergey Lapin
  • 1,008
  • 3
  • 12
  • 21
0
local openUrl = function()
    local file = vim.fn.expand("<cWORD>")
    local result = ":!open " .. file
    if
        string.match(file, "https") == "https"
        or string.match(file, "http") == "http"
    then
        vim.cmd(result)
    else
        return print(" Woops is not url gais ")
    end
end
vim.keymap.set("n", "gx", openUrl, { desc = "OpenUrl Undercurword" })
S25
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '23 at 11:24