1

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?

  • remove either ' or / from start and end of regexp – Maxim Kim Mar 27 at 10:57
  • use matchadd() family of functions. – Christian Brabandt Mar 27 at 12:48
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:]%\/_#.-]*')
| improve this answer | |
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)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.