I have a plugin which integrates a terminal that I can send commands through using a custom command I made. The only issue I'm having is that sometimes after the terminal gets clogged, it tends not to scroll to the latest input, forcing me to constantly switch to it and manually G.

Since in my setup I already have the buffer & window ID, I thought I could add extend my custom command to switch to the terminal, G, and switch back, after the command had been sent. Thing is, I haven't found any way to switch to another window solely by using its ID. How can this be done?

Alternatively, is there a way to send G straight to a window? That way I won't have to jump between them.


1 Answer 1


If you have the window ID then win_execute({winid}, {command}) should be useful. It is like execute() but operates in the context of the specified window.

Something like:

:call win_execute({winid}, '$')

$ is the EX command equivalent of the Normal mode command G, i.e. move the cursor to the end of the buffer.

Give that a try.

Oh, ran a little test and noticed a bit of a hiccup. The movement was registered but didn't display until I moved the cursor there. A workaround, if you need it, is to send a screen redraw command to the window after the movement command. For example, this worked for me...

:call win_execute({winid}, 'norm <c-l>')

You might be able to combine the two win_execute() commands into one by putting a pipe character (|) between the two EX commands in the string parameter. Something like...

:call win_execute({winid}, '$ | norm <c-l>')

But if you're interested in this you'll have to test it as I haven't tried it, yet.

And see :h :norm and :h CTRL-L, if you're unfamiliar with the mentioned EX commands.

  • Thanks, this is exactly what I needed. Worked for me even without refreshing. Is there a similar command for Neovim?
    – daedsidog
    Commented Mar 13, 2021 at 15:56
  • Cool. Not sure about nvim. Certainly not the same func. I quickly guessed at some things and found this: nvim_win_set_cursor({window}, {pos}). First param is called window "handle" in docs but I imagine it's the same thing as win id. That might do it. Or it could be equivalent to vim win number. It'll probably be pretty clear with some poking around in the help. If you get stuck go ahead and ask a new question.
    – B Layer
    Commented Mar 13, 2021 at 18:38
  • I ended up just jumping to the window, running the command, and then jumping back using wincmd w for nvim.
    – daedsidog
    Commented Mar 13, 2021 at 20:19
  • But how can you stand the inconsistency of methods across the two? ;)
    – B Layer
    Commented Mar 13, 2021 at 20:41
  • This question seems related, and there are links there that are also related. If they are dupes, we should link them together; otherwise, perhaps you have answers? :) LMK what you think re: dupes vi.stackexchange.com/q/29510/10604
    – D. Ben Knoble
    Commented Mar 14, 2021 at 16:04

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.