Text objects are one of the power of Vim that I particulary like.
I wanted to create my personnal text object with the p (="previous") and n (="next") parameters. For instance if I type din) (="delete in next closing parenthesis") vim will look for the next parenthesis (even if it's in another line below) and delete it's content.
It is simple when I want to reach the previous/next parenthesis wich is in the same line as my cursor:
"Point the previous parenthesis
onoremap ip) :<C-U>normal! F(vi(<CR>
"Point the next parenthesis
onoremap in) :<C-U>normal! f(vi(<CR>
But the problems come when I want to modifie those mapping to extend the search outside of the line.
I finally found an alternative with this structure (onoremap [keys] :[search] | [selection]<CR>
):
"Point the previous parenthesis
onoremap ip) :<C-U>call search('(','b')<Space><Bar><Space>normal! lvi)<CR>
"Point the next parenthesis
onoremap in) :<C-U>call search('(')<Space><Bar><Space>normal! lvi)<CR>
But I have to use the "search()
" function because the "/
" command doesn't seem to work.
I also need to use "<Space><Bar><Space>
" to combine two commands (work as "|
").
I see that using "<C-U>
" right after ":
" can avoid some border effect here
.
My question:
Is there a user-friendly way (a shorter and more readable answer) to do the same kind of mapping?
Personnally, text object is one of my favorite vim feature and I think it's a bit sad to struggle doing a mapping "on the fly" (but now I know that way, I can reproduce that without any problem).
<space><bar><space>
? Other than that there's not too much you can do to simplify the thing. Except move the bulk of it into a function...then the mapping is nice and short and clean.?(<cr>vib
in)
andip)
, also works with counts and takes care of many of the odd corner cases... Is your question about how to get these particular mappings? If so, then I'd recommend just getting the plug-in.<Space>
, but you do need to escape the bar with either<Bar>
or\|
. A way to avoid these would be define a function that performs the commands to implement the motion and selection, then have the mapping just call the function. In the function you don't need|
, just use separate commands on separate lines.