Skip to content

Minimal plugin allow you to open url under cursor in neovim without netrw with default browser of your system and highlight url

License

Notifications You must be signed in to change notification settings

dunix241/url-open.nvim

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This plugin enables you to effortlessly open the URL under the cursor in Neovim, bypassing the need for netrw, and instead utilizing the default browser of your system. It provides the convenience of automatically detecting and highlighting all URLs within the text content.

NOTE: Since I am the linux user so i only test this plugin on linux, if you are using macos or windows and you have any problem with this plugin, please open an issue or create a pull request to fix it

Features

  • 🎨 Automatically detect and highlight all URLs within the text content and provide visual cues when hovering over clickable URLs.
  • 🛠️ Allow for opening URLs from anywhere on a line, as long as the line contains only one URL. If one line has multiple URLs, the first URLs in the right side of cursor will be opened.
  • 🎉 Open the URLs under the cursor, including the Markdown link (e.g. https://github.com/sontungexpt/url-open).
  • ✈️ Open the GitHub page for the Neovim plugin mentioned under the cursor (e.g. Plug 'nvim-lua/plenary.nvim', "sontungexpt/url-open").
  • 🍨 Easily open the npm package specified in the package.json file. (e.g. "lodash": "^4.17.21",).
  • 🍻 Open the Homebrew formula or cask specified in the Brewfile.
  • 🍕 Open the cargo package specified in the Cargo.toml file.
  • 🚀 Provide an optional deep pattern matching feature, which can be enabled, to accurately identify and handle various URL formats, such as:

Preview

highlight-url

highlight-all-url

rec.mp4
rec.mp4

Installation

-- lazy.nvim
{
    "sontungexpt/url-open",
    event = "VeryLazy",
    cmd = "URLOpenUnderCursor",
    config = function()
        local status_ok, url_open = pcall(require, "url-open")
        if not status_ok then
            return
        end
        url_open.setup ({})
    end,
},
  • NOTE: If you want to use minimal source with no comments, no validate configs, no documents, you can use branch mini instead of main branch. Make sure you know that your config is valid
-- lazy.nvim
{
    "sontungexpt/url-open",
    branch = "mini",
    event = "VeryLazy",
    cmd = "URLOpenUnderCursor",
    config = function()
        local status_ok, url_open = pcall(require, "url-open")
        if not status_ok then
            return
        end
        url_open.setup ({})
    end,
},

Configuration

You can easily add more patterns to open url under cursor by adding more patterns to extra_patterns config

-- default values
require("url-open").setup({
    -- default will open url with default browser of your system or you can choose your browser like this
    -- open_app = "micorsoft-edge-stable",
    -- google-chrome, firefox, micorsoft-edge-stable, opera, brave, vivaldi
    open_app = "default",
    -- If true, only open the URL when the cursor is in the middle of the URL.
    -- If false, open the next URL found from the cursor position,
    -- which means you can open a URL even when the cursor is in front of the URL or in the middle of the URL.
    open_only_when_cursor_on_url = false,
    highlight_url = {
        all_urls = {
            enabled = false,
            fg = "#21d5ff", -- "text" or "#rrggbb"
            -- fg = "text", -- text will set underline same color with text
            bg = nil, -- nil or "#rrggbb"
            underline = true,
        },
        cursor_move = {
            enabled = true,
            fg = "#199eff", -- "text" or "#rrggbb"
            -- fg = "text", -- text will set underline same color with text
            bg = nil, -- nil or "#rrggbb"
            underline = true,
        },
    },
    deep_pattern = false,
    -- a list of patterns to open url under cursor
    extra_patterns = {
        -- {
        -- 	  pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"',
        -- 	  prefix = "https://www.npmjs.com/package/",
        -- 	  suffix = "",
        -- 	  file_patterns = { "package%.json" },
        -- 	  excluded_file_patterns = nil,
        -- 	  extra_condition = function(pattern_found)
        -- 	    return not vim.tbl_contains({ "version", "proxy" }, pattern_found)
        -- 	  end,
        -- },
		-- so the url will be https://www.npmjs.com/package/[pattern_found]


        -- {
        -- 	  pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"',
        -- 	  prefix = "https://www.npmjs.com/package/",
        -- 	  suffix = "/issues",
        -- 	  file_patterns = { "package%.json" },
        -- 	  excluded_file_patterns = nil,
        -- 	  extra_condition = function(pattern_found)
        -- 	    return not vim.tbl_contains({ "version", "proxy" }, pattern_found)
        -- 	  end,
        -- },
		--
		-- so the url will be https://www.npmjs.com/package/[pattern_found]/issues
    },
})

Usage

Command Description
:URLOpenUnderCursor Open url under cursor
:URLOpenHighlightAll Highlight all url in current buffer
:URLOpenHighlightAllClear Clear all highlight url in current buffer
  • This plugin will not map any key by default, you can map it by yourself
vim.keymap.set("n", "gx", "<esc>:URLOpenUnderCursor<cr>")

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details

About

Minimal plugin allow you to open url under cursor in neovim without netrw with default browser of your system and highlight url

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%