Giter Club home page Giter Club logo

url-open's Introduction

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

url-open's People

Contributors

alichtman avatar sontungexpt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

url-open's Issues

Error with `deep_pattern = true` when scrolling binary files

Opening binary file, like PNG image or WAV audio file and scrolling forward causes this error message to pop up:

Error detected while processing CursorMoved Autocommands for "*":
Error executing lua callback: Vim:E976: using Blob as a String
stack traceback:
        [C]: in function 'matchstrpos'
        ...are/nvim/lazy/url-open/lua/url-open/modules/handlers.lua:106: in function 'find_first_url_in_l
ine'
        ...are/nvim/lazy/url-open/lua/url-open/modules/handlers.lua:173: in function 'foreach_url_in_line
'
        ...re/nvim/lazy/url-open/lua/url-open/modules/highlight.lua:48: in function 'highlight_cursor_url
'
        ...hare/nvim/lazy/url-open/lua/url-open/modules/autocmd.lua:35: in function <...hare/nvim/lazy/ur
l-open/lua/url-open/modules/autocmd.lua:34>

Minimal config:

local fn = vim.fn

local lazypath = fn.stdpath("data") .. "/lazy/lazy.nvim"

if not vim.loop.fs_stat(lazypath) then
  if fn.confirm("Download and initialize the configured plugins?", "&Yes\n&No", 2) == 2 then return end
  fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath })
end

vim.opt.rtp:prepend(lazypath)

require("lazy").setup(
  {
    'sontungexpt/url-open',
    config = function()
      require("url-open").setup({
        deep_pattern = true,
      })
    end,
  }
)

nvim --version:

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1697887905

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Feature: Following MD file path links (switching editor between the linked files)

I previously used jghauser/follow-md-links.nvim plugin to open HTTP URLs in MD files.

I like url-open better in that regard, because it works for opening HTTP URLs in all files.

But from follow-md-links I miss being also able to jump in the editor between the linked files e.g. [a relative path file](./../relative_path_file.md) using the same key as opening the browser HTTP URLs.

Considering url-open already handles MD HTTP URL pattern, would you be interested to implement switching in editor between the linked files functionality in url-open?

Or maybe facilitate some way for url-open and follow-md-links to work via the same keyboard shortcut?

Possible feature requests

After developing the patterns in #10 , I ran into some things that I'd like to have some more control over, possibly.

1. Some way to control what text is highlighted, not just everything that matches the pattern

This is so that things like the gem or brew prefix don't get highlighted. I'd still like them to be part of the target area. So this is just a cosmetic difference. It's not that big of a deal, but it'd be pretty cool.

2. The ability to pass in a function to build up the URL

I could collapse the two Dockerfile related sections in my other issue if I could control how the URL is built with a function. I could even extend it further and support custom registries fully, by calling a system command to fetch the appropriate URL.

The default version of the function should stick with the current prefix & suffix, it's good enough 90% of the time. This is more of an edge case.

3. The ability to include multiple patterns to match within a file

This is probably pretty hard, from a quick scan through the code. But, being able to include multiple patterns would be a very powerful combo with idea 2.


What do you think? I'm familiar with Lua, so I could take a shot at some of these ideas, but would like your opinion.

Some additional patterns you might be interested in

Here are some patterns I've created after having this installed for only a few hours, it's been awesome!

To jump to Rubygems from a Gemfile (or it's alternative name, gems.rb):

{                                           
  pattern = "gem ['\"]([^%s]*)['\"]",       
  prefix = "https://rubygems.org/gems/",    
  suffix = "",                              
  file_patterns = { "Gemfile", "gems.rb" }, 
  excluded_file_patterns = nil,             
},                                          

Or to load the URL for a base image in Dockerhub:

-- Dockerfile images: unprefix, unnamespaced (the library images, like `ruby:3.2`) 
-- results in: https://hub.docker.com/_/ruby/                                      
{                                                                                  
  pattern = "^FROM ([^:.]+):",                                                     
  prefix = "https://hub.docker.com/_/",                                            
  suffix = "/",                                                                    
  file_patterns = { "Dockerfile%S*", "Containerfile%S*" },                         
  excluded_file_patterns = nil,                                                    
  extra_condition = function(matched_pattern)                                      
    return not matched_pattern:match("/")                                          
  end,                                                                             
},                                                                                 
-- Dockerfile images: unprefixed but namespaced (like crystallang/crystal)         
-- results in: https://hub.docker.com/r/crystallang/crystal                        
{                                                                                  
  pattern = "FROM ([^:.]+):",                                                      
  prefix = "https://hub.docker.com/r/",                                            
  suffix = "/",                                                                    
  file_patterns = { "Dockerfile%S*", "Containerfile%S*" },                         
  excluded_file_patterns = nil,                                                    
  extra_condition = function(matched_pattern)                                      
    return matched_pattern:match("/")                                              
  end,                                                                             
},

Doesn't seem to recognize URLs in a JS file

Hi, not sure if this happened recently or was it always there and I didn't notice.

But today, I opened a .js file that has a URL as a comment, but when I try to open the URL, I get the "no url found" message

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.