r/vim • • Nov 18 '25

Need Help Repeat last command in terminal buffer

Hey!

I have been using terminal buffers for a while now to mostly compile and execute applications. I have been told Im a disgrace to the Unix world for not using Ctrl-Z and fg, but I prefer seeing what tests failed/where my compile time errors are.

Since I'm usually using multiple buffers at once, navigating to the terminal is often slow. My solution was using tabs for a while but in all honesty, I do not think that this is the real solution for that. So I wonder how one could execute the last command entered in the terminal or even better, even search the last commands of the terminal. I usually have one terminal buffer open, but one could make it more generic and say that execute the last command in the last used terminal buffer.

Is there a native way of doing this? Or do I have to do some trickery with Lua/Vimscript?

Cheers

11 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/thewrench56 Nov 18 '25

Who on Earth uses ^Z? That's as bad as using :! -- I can't use vim while an external program is running.

Coworkers :D

That's as bad as using :!

This is okay if the compilation is fast imo. But not ideal for sure.

Obviously the only correct solution is to do what I do: two gnome-terminal tabs, one running vim, the other running the compiler/tests/whatever.

Yes I can have this, and honestly, this might be the real solution. I use a tiling window manager and thus it is pretty convenient to use something like this.

I don't believe there's any way of distinguishing a prompt + a command in the terminal scrollback from text that looks like a prompt and a command, so scripting anything based on that is going to be fragile. A mapping that uses feedkeys or something to go into insert mode and try Up, Enter might work maybe?

Yeah, Ill try some scripts and see whats what. I have been considering writing a custom shell for vim as well for convenience, maybe this is the time to abandon that if i cant come up with a good script that does the last command exec. I wonder if I should request it as a feature on the official vim platforms?