[vim/vim] feature request: gv in operator pending mode (#3666)

16 views
Skip to first unread message

Sha Liu

unread,
Dec 5, 2018, 9:03:35 PM12/5/18
to vim/vim, Subscribed

vim already have a very nice gn mapping, which works for normal, visual and as well as operator-pending. But gv cannot be used in operator pending mode. To achieve this, we need to enter g@:normal gv<cr>.

Due to one limitation of the :normal commands in operator pending mode, the motion-wiseness cannot be forced to a different one if it selects visual area. By introducing gv as an textobject, we do not only save many typings, but also bypass the limitation above. We will easily get g@vgv, g@Vgv, g@<c-v>gv to have different motion-wiseness.

With this, the creation of new textobject will be much easier and consistent. For example, we can define the text object as follows

function! MyTextObject()
   call setpos("'<", position1)
   call setpos("'>", position2)
   let deisred_motionwiseness = 'v'    " can be others depending the nature of the textobject
   let change = visualmode() == deisred_motionwiseness ? deisred_motionwiseness : ''
   return "gv" . change
endfunction
omap <expr> mto MyTextObject()

Here change is used to set the motion-wiseness of the visual area to be the desired one while the last visual mode (as used by gv) may be different. (For example, if the previous visual area is linewise but MyTextObject should be characterwise, gv will still be linewise whlie gvv will be character wise; like what happens in normal mode.)

Now come to the user, they are still free to type g@Vmto or g@<c-v>mto to force a different motion-wiseness other than the desired one as provided by the plugin author.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

Christian Brabandt

unread,
Dec 6, 2018, 2:24:26 AM12/6/18
to vim/vim, Subscribed
function! MyTextObject()
   call setpos("'<", position1)
   call setpos("'>", position2)
   let deisred_motionwiseness = 'v'    " can be others depending the nature of the textobject
   let change = visualmode() == deisred_motionwiseness ? deisred_motionwiseness : ''
   return "gv" . change
endfunction
omap <expr> mto MyTextObject()

I think this works already. If you make
:let desired_motionwiseness=":norm! \<c-v>\<esc>" (use v or V for the other motions)
and then use the setpos() calls, gv should already work, right?

Sha Liu

unread,
Dec 6, 2018, 8:21:31 AM12/6/18
to vim/vim, Subscribed

Sorry I didn't get your point. What is to be returned? Would you please post the complete code?

Christian Brabandt

unread,
Dec 6, 2018, 8:43:53 AM12/6/18
to vim/vim, Subscribed

Well, you want gv either characterwise, linewise, or blockwise, right? So first select a single region either characterwise, linewise, or blockwise. Then use setpos() to set the '< and '> marks and finally use gv to have the new region selected. Should work:

function! MyTextObject()
   exe "norm! v\<esc>"   " make the selection characterwise and end it immediately
   call setpos("'<", position1)
   call setpos("'>", position2)
   return "gv"
endfunction

Sha Liu

unread,
Dec 6, 2018, 8:50:37 AM12/6/18
to vim/vim, Subscribed

This is a good point to omit the use of change. But it still returns "gv" but gv cannot used as an motion/textobject. Because dgv does not work, neither does dmto (mto: MyTextObject); not to mention dVmto and d<c-v>mto.

To make them work, gv should be able to be used in operator pending mode, which is what I am requesting.

Christian Brabandt

unread,
Dec 6, 2018, 8:55:25 AM12/6/18
to vim/vim, Subscribed

Ah, now I understand.

Sha Liu

unread,
Dec 6, 2018, 8:58:27 AM12/6/18
to vim/vim, Subscribed

To clarify: I want gv to be used in operator-pending mode, like what gn does.

Sha Liu

unread,
Dec 6, 2018, 9:01:34 AM12/6/18
to vim/vim, Subscribed

BTW, is there any reason that visualmode() is a function instead of a variable like v:visualmode so that it can be both read and write?

Is it possible/easy to make it an variable? If yes, the we can simply do let v:visualmode = desired_motionwiseness

Christian Brabandt

unread,
Jun 16, 2025, 2:22:27 PM6/16/25
to vim/vim, Subscribed

Closed #3666 as completed via cb27992.


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/3666/issue_event/18175868192@github.com>

Reply all
Reply to author
Forward
0 new messages