Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created February 9, 2024 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRadev/0c6a7d0a9d56b25008237736625f373d to your computer and use it in GitHub Desktop.
Save AndrewRadev/0c6a7d0a9d56b25008237736625f373d to your computer and use it in GitHub Desktop.
URL text object
onoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr>
xnoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr>
onoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr>
xnoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr>
function! s:UrlTextObject()
let saved_view = winsaveview()
let saved_end_pos = getpos("'b")
defer setpos("'e", saved_end_pos)
let cursor_col = col('.')
let url_pattern = highlighturl#default_pattern()
let [line, end_col] = searchpos(url_pattern, 'Wce', line('.'))
if line <= 0
" No match found on the line:
call winrestview(saved_view)
return
endif
call setpos("'e", getpos('.'))
let [_, start_col] = searchpos(url_pattern, 'Wbc', line('.'))
if start_col > cursor_col
" The cursor was not on the URL
call winrestview(saved_view)
return
endif
normal! v`e
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment