diff --git a/README.md b/README.md index 9f884e6..faa1f59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,28 @@ # url.nvim -A smol neovim plugin to open a url you are standing on in your browser \ No newline at end of file +A smol neovim plugin to open the url under the cursor, in your browser. + +## Installation +With Lazy: +```lua +{ "https://code.smolnet.org/micke/url.nvim" } +``` +## Usage +``` +:UrlOpen +``` +## Keybindings +Add a keybinding: +``` +nnoremap u :UrlOpen +``` +or with lua +```lua +vim.api.nvim_set_keymap("n", "u", "UrlOpen", {}) +``` +or using which-key.nvim +```lua +require("which-key").add({ + {"u", "UrlOpen", desc = "Open URL"} +} +``` diff --git a/lua/url.lua b/lua/url.lua new file mode 100644 index 0000000..13632ff --- /dev/null +++ b/lua/url.lua @@ -0,0 +1,24 @@ + +local get_url = function() + local line = vim.api.nvim_get_current_line() + local window = vim.api.nvim_get_current_win() + local _,col = unpack(vim.api.nvim_win_get_cursor(window)) + + local urls = line:gmatch("%w+://[%w_%.%-]+[%w_.@~%-%/%?&#=]*") + for url in urls do + if col >= string.find(line, url) and col <= string.find(line, url) + #url then + return url + end + end +end + + + +local M = {} + +M.open_url = function() + local url = get_url() + if url then vim.ui.open(url) end +end + +return M diff --git a/plugin/url.lua b/plugin/url.lua new file mode 100644 index 0000000..3ba6f97 --- /dev/null +++ b/plugin/url.lua @@ -0,0 +1,13 @@ +local callback = function() + local url = require('.url') + url.open_url() +end + +vim.api.nvim_create_user_command( + "UrlOpen", + callback, + { + nargs = 0, + desc = "Open a URL in the default browser", + } +)