Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active July 9, 2025 12:46
Show Gist options
  • Save AndrewRadev/25351d84a12d961f33e087fda2a6be6c to your computer and use it in GitHub Desktop.
Save AndrewRadev/25351d84a12d961f33e087fda2a6be6c to your computer and use it in GitHub Desktop.
Custom <> text object
" Place mappings in:
" ~/.vim/ftplugin/javascriptreact.vim: For JSX
" ~/.vim/ftplugin/typescriptreact.vim: For TSX
onoremap <buffer> a< :<c-u>call <SID>TagTextObject('a')<cr>
xnoremap <buffer> a< :<c-u>call <SID>TagTextObject('a')<cr>
onoremap <buffer> i< :<c-u>call <SID>TagTextObject('i')<cr>
xnoremap <buffer> i< :<c-u>call <SID>TagTextObject('i')<cr>
onoremap <buffer> a> :<c-u>call <SID>TagTextObject('a')<cr>
xnoremap <buffer> a> :<c-u>call <SID>TagTextObject('a')<cr>
onoremap <buffer> i> :<c-u>call <SID>TagTextObject('i')<cr>
xnoremap <buffer> i> :<c-u>call <SID>TagTextObject('i')<cr>
function! s:TagTextObject(mode) abort
if a:mode == 'a'
let start_pattern = '<'
let end_pattern = '[^=]\zs>'
else " a:mode == 'i'
let start_pattern = '<\zs\S'
let end_pattern = '[^=]>'
endif
" Find the start of the <tag>:
if searchpair(start_pattern, '', end_pattern, 'Wbc') <= 0
return
endif
" Save start position in "t" mark:
normal! mt
" Find the end of the <tag>:
call searchpair(start_pattern, '', end_pattern, 'W')
" Visual-select from here to the start of the tag:
normal! v`t
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment