Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Last active May 19, 2021 17:40
Show Gist options
  • Save PeterRincker/00c245a1bbf9cb0e8847c9c95f4f2726 to your computer and use it in GitHub Desktop.
Save PeterRincker/00c245a1bbf9cb0e8847c9c95f4f2726 to your computer and use it in GitHub Desktop.
Basic textobject support for variable segments
" Basic support for variable segment textobjects
" e.g. "foo" in foo_var and fooBar
xnoremap iv :<c-u>call <SID>segment()<cr>
onoremap iv :normal viv<cr>
xnoremap av :<c-u>call <SID>segment(1)<cr>
onoremap av :normal vav<cr>
function s:segment(...)
let start_pat = '\C\v%(<|_+\zs\w|[a-z0-9]\zs[A-Z])'
let end_pat = '\C\v%(.>|\w\ze_+|[a-z0-9]\ze[A-Z])'
call search(start_pat, 'bc')
normal! m<
call search(end_pat, 'ce')
normal! m>
if a:0
call s:an([
\ {'mark': '>', 'pat':'_+'},
\ {'mark': '<', 'pat':'_+'},
\ ])
endif
normal! gv
endfunction
function s:an(attemps) abort
let view = winsaveview()
for item in a:attemps
let is_bwd = item.mark == '<'
execute 'normal! `'. item.mark
let pat = '\C\v' . printf(is_bwd ? '%s%%#' : '%%#.%s', item.pat)
let flags = is_bwd ? 'b' : 'ce'
if search(pat, flags) != 0
execute 'normal! m' . item.mark
return
endif
endfor
call winsaveview()
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment