Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active January 21, 2025 10:40

Revisions

  1. Konfekt revised this gist Jan 21, 2025. 2 changed files with 1 addition and 55 deletions.
    1 change: 1 addition & 0 deletions better_gx.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This has been included in `:help netrw-handler`
    55 changes: 0 additions & 55 deletions better_gx.vim
    Original file line number Diff line number Diff line change
    @@ -1,55 +0,0 @@
    " Better gx to open URLs.
    "
    " Adapted from https://github.com/habamax/.vim/blob/d5087779fee4a7372c1dd2c407674997fdb1268d/autoload/os.vim
    " to use in particular :Open command from https://gist.github.com/Konfekt/8e484af2955a0c7bfe82114df683ce0f
    " See also https://gist.github.com/AndrewRadev/1ba9eba62df82d616fc8040338c4c4e1

    " URL regexes
    let s:regex_url = '\%(\%(http\|ftp\|irc\)s\?\|file\)://\S\{-}'

    func! s:GetURL() abort
    " markdown URL such as [link text](http://ya.ru 'yandex search')
    try
    let save_view = winsaveview()
    if searchpair('\[.\{-}\](', '', ')\zs', 'cbW', '', line('.')) > 0
    return matchstr(getline('.')[col('.')-1:], '\[.\{-}\](\zs'..s:regex_url..'\ze\(\s\+.\{-}\)\?)')
    endif
    finally
    call winrestview(save_view)
    endtry
    " HTML URL such as <a href='http://www.python.org'>Python is here</a>
    " <a href="http://www.python.org"/>
    try
    let save_view = winsaveview()
    if searchpair('<a\s\+href=', '', '\%(</a>\|/>\)\zs', 'cbW', '', line('.')) > 0
    return matchstr(getline('.')[col('.') - 1 : ],
    \ 'href=["'.."'"..']\?\zs\S\{-}\ze["'.."'"..']\?/\?>')
    endif
    finally
    call winrestview(save_view)
    endtry
    " URL with leading or closing parenthesis (http://google.com)
    let URL = matchstr(expand("<cWORD>"), '^(\?\zs'..s:regex_url..'\ze)\?$')
    if !empty(URL) | return URL | endif

    " Is it a file in the current work dir?
    let file = expand("<cfile>")
    if filereadable(file) | return file | endif
    " .. or that of the current buffer?
    let path = expand('%')
    if isdirectory(path)
    let dir = fnamemodify(path, ':p')
    elseif filereadable(path)
    let dir = fnamemodify(path, ':p:h')
    endif
    if exists('dir') && filereadable(dir..'/'..file) | return dir..'/'..file | endif

    return ''
    endf

    func! s:BetterGx() abort
    let URL = s:GetURL()
    if !empty(URL) | exe 'Open' escape(URL, '#%') | endif
    endfunc

    nnoremap <silent> gx :<c-u>call <SID>BetterGx()<CR>
  2. Konfekt revised this gist Sep 27, 2024. No changes.
  3. Konfekt revised this gist Sep 25, 2024. 1 changed file with 30 additions and 44 deletions.
    74 changes: 30 additions & 44 deletions better_gx.vim
    Original file line number Diff line number Diff line change
    @@ -1,69 +1,55 @@
    " Better gx to open URLs in Vim.
    " Better gx to open URLs.
    "
    " Adapted from https://github.com/habamax/.vim/blob/d5087779fee4a7372c1dd2c407674997fdb1268d/autoload/os.vim
    " to use in particular :Open command from https://gist.github.com/Konfekt/8e484af2955a0c7bfe82114df683ce0f
    " See also https://gist.github.com/AndrewRadev/1ba9eba62df82d616fc8040338c4c4e1

    func! s:BetterGx() abort
    " URL regexes
    let rx_base = '\%(\%(http\|ftp\|irc\)s\?\|file\)://\S'
    let rx_bare = rx_base . '\+'
    let rx_embd = rx_base . '\{-}'

    let URL = ''
    " URL regexes
    let s:regex_url = '\%(\%(http\|ftp\|irc\)s\?\|file\)://\S\{-}'

    " cursor on
    func! s:GetURL() abort
    " markdown URL such as [link text](http://ya.ru 'yandex search')
    try
    let save_view = winsaveview()
    if searchpair('\[.\{-}\](', '', ')\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.')-1:], '\[.\{-}\](\zs'.rx_embd.'\ze\(\s\+.\{-}\)\?)')
    return matchstr(getline('.')[col('.')-1:], '\[.\{-}\](\zs'..s:regex_url..'\ze\(\s\+.\{-}\)\?)')
    endif
    finally
    call winrestview(save_view)
    endtry

    " asciidoc URL such as http://yandex.ru[yandex search]
    if empty(URL)
    try
    let save_view = winsaveview()
    if searchpair('\[.\{-}\](', '', ')\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.') - 1 : ], '\[.\{-}\](\zs' . rx_embd . '\ze\(\s\+.\{-}\)\?)')
    endif
    finally
    call winrestview(save_view)
    endtry
    endif

    " HTML URL such as <a href='http://www.python.org'>Python is here</a>
    " <a href="http://www.python.org"/>
    if empty(URL)
    try
    let save_view = winsaveview()
    if searchpair('<a\s\+href=', '', '\%(</a>\|/>\)\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.') - 1 : ],
    \ 'href=["' . "'" . ']\?\zs\S\{-}\ze["' . "'" . ']\?/\?>')
    endif
    try
    let save_view = winsaveview()
    if searchpair('<a\s\+href=', '', '\%(</a>\|/>\)\zs', 'cbW', '', line('.')) > 0
    return matchstr(getline('.')[col('.') - 1 : ],
    \ 'href=["'.."'"..']\?\zs\S\{-}\ze["'.."'"..']\?/\?>')
    endif
    finally
    call winrestview(save_view)
    endtry
    " URL with leading or closing parenthesis (http://google.com)
    let URL = matchstr(expand("<cWORD>"), '^(\?\zs'..s:regex_url..'\ze)\?$')
    if !empty(URL) | return URL | endif

    " Is it a file in the current work dir?
    let file = expand("<cfile>")
    if filereadable(file) | return file | endif
    " .. or that of the current buffer?
    let path = expand('%')
    if isdirectory(path)
    let dir = fnamemodify(path, ':p')
    elseif filereadable(path)
    let dir = fnamemodify(path, ':p:h')
    endif
    if exists('dir') && filereadable(dir..'/'..file) | return dir..'/'..file | endif

    " URL (http://google.com)
    if empty(URL)
    let URL = matchstr(expand("<cWORD>"), '^(\zs' . rx_bare . '\ze)$')
    endif

    " barebone URL http://google.com
    if empty(URL)
    let URL = matchstr(expand("<cWORD>"), rx_bare)
    endif

    if empty(URL)
    return
    endif
    return ''
    endf

    exe 'Open! ' . escape(URL, '#%')
    func! s:BetterGx() abort
    let URL = s:GetURL()
    if !empty(URL) | exe 'Open' escape(URL, '#%') | endif
    endfunc

    nnoremap <silent> gx :<c-u>call <SID>BetterGx()<CR>
  4. Konfekt created this gist Sep 18, 2024.
    69 changes: 69 additions & 0 deletions better_gx.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    " Better gx to open URLs in Vim.
    "
    " Adapted from https://github.com/habamax/.vim/blob/d5087779fee4a7372c1dd2c407674997fdb1268d/autoload/os.vim
    " to use in particular :Open command from https://gist.github.com/Konfekt/8e484af2955a0c7bfe82114df683ce0f
    " See also https://gist.github.com/AndrewRadev/1ba9eba62df82d616fc8040338c4c4e1

    func! s:BetterGx() abort
    " URL regexes
    let rx_base = '\%(\%(http\|ftp\|irc\)s\?\|file\)://\S'
    let rx_bare = rx_base . '\+'
    let rx_embd = rx_base . '\{-}'

    let URL = ''

    " cursor on
    " markdown URL such as [link text](http://ya.ru 'yandex search')
    try
    let save_view = winsaveview()
    if searchpair('\[.\{-}\](', '', ')\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.')-1:], '\[.\{-}\](\zs'.rx_embd.'\ze\(\s\+.\{-}\)\?)')
    endif
    finally
    call winrestview(save_view)
    endtry

    " asciidoc URL such as http://yandex.ru[yandex search]
    if empty(URL)
    try
    let save_view = winsaveview()
    if searchpair('\[.\{-}\](', '', ')\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.') - 1 : ], '\[.\{-}\](\zs' . rx_embd . '\ze\(\s\+.\{-}\)\?)')
    endif
    finally
    call winrestview(save_view)
    endtry
    endif

    " HTML URL such as <a href='http://www.python.org'>Python is here</a>
    " <a href="http://www.python.org"/>
    if empty(URL)
    try
    let save_view = winsaveview()
    if searchpair('<a\s\+href=', '', '\%(</a>\|/>\)\zs', 'cbW', '', line('.')) > 0
    let URL = matchstr(getline('.')[col('.') - 1 : ],
    \ 'href=["' . "'" . ']\?\zs\S\{-}\ze["' . "'" . ']\?/\?>')
    endif
    finally
    call winrestview(save_view)
    endtry
    endif

    " URL (http://google.com)
    if empty(URL)
    let URL = matchstr(expand("<cWORD>"), '^(\zs' . rx_bare . '\ze)$')
    endif

    " barebone URL http://google.com
    if empty(URL)
    let URL = matchstr(expand("<cWORD>"), rx_bare)
    endif

    if empty(URL)
    return
    endif

    exe 'Open! ' . escape(URL, '#%')
    endfunc

    nnoremap <silent> gx :<c-u>call <SID>BetterGx()<CR>