Wiener Boat
Wiener Boat

Reputation: 446

Replacing a motion in operator mode

I'm trying to alter the ge motion's functionality when it's used with an operator. Instead of it operating on the last character of the target word, I'd like it to work exclusively only until the target word's last character similarly to w.

Example:

foo bar
     ^

Should lead to fooar instead of foar when I use dge with the cursor at ^

I don't know anything about Vimscript and I'm pretty much relying on exe now. This quick attempt I wrote seems to be full of errors. The variables don't seem to be working right at all.

My leading idea is to escape when ge is typed in operator mode and call the function with hopefully the right arguments. Then set a mark on the starting position, move to the left one column (to ensure that it would work with repeated movements, even though this is operator mode only), move the set amount of ges and then move one column right (my main goal here) and delete till the set mark.

If anybody could point out the errors I've made, it would be greatly appreciated!

function! FixGE(count, operator)
    exe 'normal m['
    exe 'normal h'
    exe count.'normal ge'
    exe 'normal l'
    exe operator.'normal `['
endfunction
onoremap ge <esc>:call FixGE(v:prevcount, v:operator)<cr>

Upvotes: 2

Views: 183

Answers (2)

benjifisher
benjifisher

Reputation: 5112

This is a little closer (untested):

function! FixGE(count, operator)
    exe 'normal m['
    exe 'normal h'
    exe 'normal ' . a:count . 'ge'
    exe 'normal l'
    exe 'normal ' . a:operator . '`['
endfunction
onoremap ge <esc>:call FixGE(v:prevcount, v:operator)<cr>

Function arguments have to use the :a prefix (or sigil, I think). I know that count is a reserved variable: :let count = 7 gives an error. I think it is OK to use it as a function argument, though.

I have also put :normal at the beginning of two lines, instead of in the middle.

Upvotes: 1

mMontu
mMontu

Reputation: 9273

Your example could be solved without changing the ge motion, by using the f and t operators:

  • dTo
  • dF (d + F + <space> )

If you still feels that you need to override the default behavior of ge you shoul check Ingo Karkat's plugin, CountJump : Create custom motions and text objects via repeated jumps:

DESCRIPTION
Though it is not difficult to write a custom movement (basically a :map
that executes some kind of search or jump) and a custom text-object (an
:omap that selects a range of text), this is too complex for a novice user
and often repetitive. 

Upvotes: 1

Related Questions