2

I'm trying to make it so urls are highlighted and a shortcut can be used when the cursor is on top of one to open it in browser. Added this to a syntax file:

syn match matchURL '/http[s]\?:\/\/[[:alnum:]%\/_#.-]*/'
hi matchURL ctermfg=14

Tested with https://www.archlinux.org but it didn't get highlighted. (Ofc I tested simpler syn matches and highlights to ensure that syntax highlighting is working, so I conclude the regex is probably wrong?). How to make highlight urls then?

Also once urls are highlighted, is there a simple way to copy to a register/variable the text that is highlighted under the cursor?

2
  • remove either ' or / from start and end of regexp
    – Maxim Kim
    Commented Mar 27, 2020 at 10:57
  • use matchadd() family of functions. Commented Mar 27, 2020 at 12:48

2 Answers 2

1

You have extra quotes in your regex, it should look like:

syn match matchURL /http[s]\?:\/\/[[:alnum:]%\/_#.-]*/
hi matchURL ctermfg=14

Then, to save the match under the cursor in a variable, you can do:

let link = expand('<cWORD>')
let link = matchstr(link, 'http[s]\?:\/\/[[:alnum:]%\/_#.-]*')
1
  • syn match matchURL /http[s]\?:\/\/[[:alnum:]%\/_#.-]*/ not working, should be only match matchURL /http[s]\?:\/\/[[:alnum:]%\/_#.-]*/, it worked liked this for me.
    – alexzander
    Commented Jun 8, 2021 at 19:16
1

To highlight URL, you can use vim-highlighturl, which can highlight all kinds of URLs.

To open URL in browser, you can use open-browser.vim and define the following shortcut to open URL in browser:

nmap ob <Plug>(openbrowser-smart-search)
vmap ob <Plug>(openbrowser-smart-search)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.