1

The jumplist commands <C-o>, <C-i> and change list commands g;, g, don't seem to work in visual mode (tested in vim -u NONE).

How can we implement this?

My attempt so far with:

vnoremap <C-o> :<C-u>normal gv<C-o><CR>

hasn't worked (I get "mapping is too recursive"; I guess normal activates the visual mapping after gv, which defeats the purpose of the mapping).

I'm not very sure what's the right way to map this.

1
  • Do you want to extend the visual selection to the nth last jump or nth last change, as in v<C-o><C-o><C-o>? Going up or down the change list might be relatively simple but the jump list spans files so it will be trickier.
    – romainl
    Commented Nov 17 at 6:58

1 Answer 1

0

Going up or down the change list in visual mode is relatively trivial:

xnoremap g; <C-c>g;v'>
xnoremap g, <C-c>g,v'>

Where…

  • <C-c> leaves visual mode without triggering side effects,
  • g;/g, jumps to the next [count] older/newer change,
  • v'> makes a visual selection from the cursor to the end of the previous visual selection.

I wrote "relatively" because always using '> seems shortsighted. You would want to compare line numbers before doing v'> or v'<. See :help getchangelist().

Also, you would want to reuse the same v/V/<C-v> instead of always using v. See :help visualmode().

The jump list is trickier because it is not limited to the current buffer. It means that you would also need to check if [count]<C-o>/[count]<C-i> jumps to another buffer before actually using <C-o>/<C-i>. See :help getjumplist().

Remarks:

  • Navigating the jump list or the change list creates even more contexts in addition to those you already have to handle (data flow, code path, project layout, cursor position, current method, etc.) Be careful, it can and will be overwhelming.

  • You didn't mention a [count] in your question so I'm wondering if you actually want <C-o>/<C-i> and g;/g,, meaning the ability to go to the 5th previous change or 3rd next jump, or if you would be content with :help '[/:help '] and :help '', which can already be used without mapping:

    v'[
    V''
    
3
  • Well I'd like to have counts if possible, since they work in normal mode, intuitively I'd like to have an implementation in visual mode that does something similar. I see the general outline of your solution, and I'll give it a try. The part about adapting for the three visual modes is simple enough to handle, and comparing `< and `> is tedious but nothing hard, however, ... Commented Nov 17 at 16:34
  • I'm more skeptical about the part where the cursor is restored. In my experience trying to implement visual mode bindings, trying to quit visual mode and manually do selections generally mess up which cursor position the user was last in within visual mode (beginning or end + top-right or bottom-left with C-v). Commented Nov 17 at 16:34
  • That's the same kind of calculation, just adding a comparison with getpos('.') to the mix.
    – romainl
    Commented Nov 18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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