Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
next/last text objects
" Next and Last {{{
" Motion for "next/last object". "Last" here means "previous", not "final".
" Unfortunately the "p" motion was already taken for paragraphs.
" Next acts on the next object of the given type in the current line, last acts
" on the previous object of the given type in the current line.
" Currently only works for (, [, {, b, r, B, ', and ".
" Some examples (C marks cursor positions, V means visually selected):
" din' -> delete in next single quotes foo = bar('spam')
" C
" foo = bar('')
" C
" canb -> change around next parens foo = bar('spam')
" C
" foo = bar
" C
" vil" -> select inside last double quotes print "hello ", name
" C
" print "hello ", name
onoremap an :<c-u>call <SID>NextTextObject('a', 'f')<cr>
xnoremap an :<c-u>call <SID>NextTextObject('a', 'f')<cr>
onoremap in :<c-u>call <SID>NextTextObject('i', 'f')<cr>
xnoremap in :<c-u>call <SID>NextTextObject('i', 'f')<cr>
onoremap al :<c-u>call <SID>NextTextObject('a', 'F')<cr>
xnoremap al :<c-u>call <SID>NextTextObject('a', 'F')<cr>
onoremap il :<c-u>call <SID>NextTextObject('i', 'F')<cr>
xnoremap il :<c-u>call <SID>NextTextObject('i', 'F')<cr>
function! s:NextTextObject(motion, dir)
let c = nr2char(getchar())
if c ==# "b"
let c = "("
elseif c ==# "B"
let c = "{"
elseif c ==# "r"
let c = "["
exe "normal! ".a:dir.c."v".a:motion.c
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.