I would define the iq
text object:
vnoremap iq ?```.*$?1<CR>o/```/-1<CR>$
omap iq :normal Viq<CR>
Where the v
mapping does:
- Search backward the triple quotes and move to the next line:
?```.*$<CR>?1
- Select the other side of the selection:
o
- Search forward the triple quotes and move to the previous line:
/```/-1<CR>
- Go to the end of the line:
$
And the do the following mapping:
nmap <leader>s7 <Plug>SlimeMotionSendiq
Here is another version that leave the /
register unchanged:
function! VisualCodeBlock()
call search('```', 'b')
normal! j0v
call search('```')
normal! k$
endfunction
vnoremap iq :<C-u>call VisualCodeBlock()<CR>
omap iq :normal Viq<CR>
Remarks:
In a standard omap
the start of the text object is the cursor position and end of the text object is the position of the cursor after the mapping has been executed.
If you need a text object that doesn't start with the cursor position you need to switch in visual mode and make the selection covering the range you want.
Usually in such case two mappings are defined such that the text object can also be used in visual
mode.
Remark <C-u>
:
The <C-u>
trick is usually used when a mapping use the Command
mode to kill the existing character in the command. If the user select some line before triggering the mapping a range will present when the mapping triggers :