0

I'm trying to create my own mapping with motion in Vim after reading this stackexchange question. I'm using the plugin vimtux to send code to specific tmux instances and I now want to be able to do this using motion instead of making a detour via visual mode. What I have so far in my .vimrc:

nmap <silent> <leader>c :set opfunc=SendWithMotion<CR>g@
function! SendWithMotion(type)
  silent exec 'normal! `[v`]y`]'
  call SendToTmux(@")
  call ExecuteKeys('Enter')
endfunction

This is working great for motions like \cw or \cap, however if I use \cip, the last line of the paragraph is captured only as its first character. E.g.:

abcd
abcd
abcd

results in sent code:

abcd
abcd
a

What could be the issue here? Sorry if this has some stupid solution, I'm really new to Vim! But I already love it :-)

1

Obviously, v means charwise selection, while you've got the linewise one (ip). So you must check the value of a:type and build the command in the right way with the help of :execute or plain :if etc.

1
  • ok, I'll dig into charwise and linewise checking of my argument and if else statements then... – Chris Feb 24 at 14:37
0

I thought it might be helpful for others if I posted my final solution to my initial problem, although my directly asked question has been answered. With the help of Matt's obviously correct answer, I changed (copied from existing plugins) my final function to:

function! SendWithMotion(type)
  if a:type == 'line'
    let lines = { 'start': line("'["), 'end': line("']") }
    silent exe lines.start . "," . lines.end . "y" 
    silent exe "normal! `]j0"
  else
    silent exe "normal! `[v`]y`]"
  endif
  call SendToTmux(@")
  call ExecuteKeys('Enter')
endfunction

which seems, is now behaving as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.