3

In a terminal buffer, I want to auto-exit when I press "kk". Except for when I'm in some interactive session, like a pager (like the pager which is created when you git diff).

To do that, I have this code

let g:_pager_bottom_texts = [":", "?", "/", "Pattern not found  (press RETURN)", "(END)"]



function! MoveIfNotInPager()
    let l:current_line = getline(".")

    if index(g:_pager_bottom_texts, l:current_line) >= 0
        normal i
        call timer_start(0, {-> feedkeys("k")})
    endif
endfunction


tnoremap kk <C-\><C-n>:call MoveIfNotInPager()<CR>

And it works moderately well, actually. I've noticed though that, in a pager, pressing k is about 1/2 speed compared to j when I'm in a terminal buffer. Can anyone recommend a better way which still gets what I'm looking for without causing terminal or pager experience to be slower?

9
  • Isn't this just a matter of conflicting mapped keys? If you change to, say, <leader>k instead of kk does the problem go away?
    – B Layer
    Commented Mar 14, 2021 at 19:24
  • No, it's definitely not a conflicting mapping issue. You can reproduce the slowness by changing kk to anything (<leader>k like you suggested, pp, or literally anything). Now open a pager and observe how it's slower to scroll up than it is to scroll down with j. I'm not really sure what exactly causes the slowness. But it's probably because the timer_start line isn't as efficient as it could be. Commented Mar 15, 2021 at 5:10
  • Okay. I had to ask..."1/2 speed" was ambiguous and thus that choice of keys suggested something along those lines. You might want to clarify that it refers to scroll speed in the question itself.
    – B Layer
    Commented Mar 15, 2021 at 7:50
  • Why a timer? What about term_sendkeys?
    – D. Ben Knoble
    Commented Mar 15, 2021 at 14:31
  • Actually, your question title doesn’t match with the body. The title talks about exiting, the code doesn’t do it, and the question in the body is about the slower key-presses. Can you clarify by editing what exactly it is your question is?
    – D. Ben Knoble
    Commented Mar 15, 2021 at 14:32

1 Answer 1

0

I didn't find a proper solution for this problem. However, if you stack two "k" calls together, the 1/2 speed becomes full speed again.

So I ended up with this

let g:_pager_bottom_texts = [":", "?", "/", "Pattern not found  (press RETURN)", "(END)"]


" If the user is in a pager, re-enter the terminal buffer and scroll up
function! MoveIfInPager()
    let l:current_line = getline(".")

    " re-enter the terminal buffer and press "k" to scroll up in the pager
    if index(g:_pager_bottom_texts, l:current_line) >= 0
        normal i

        " We send 2 "k" keys, so it scrolls at double-speed
        call timer_start(0, {-> feedkeys("k")})
        call timer_start(10, {-> feedkeys("k")})  " 10 happened to be a decent number
    endif
endfunction

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.