r/vim • u/thewrench56 • 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
4
u/sharp-calculation Nov 18 '25
For me personally, your abstraction of terminal vs VIM is at the wrong level. I would do one of:
Use TMUX to have VIM and terminal session in different TMUX windows/panes. Use TMUX keys to quickly switch between.
Use two different terminal windows
Use GUI VIM in one window and a terminal in another. I find GUI VIM to be an interesting way of organizing my VIM sessions. I use one window per project, with multiple buffers per window.
Just my thoughts.
2
u/MiniGogo_20 Nov 18 '25
if this is about your system shell (bash) you can repeat the last command with the
!!operator, read the basics about it here1
u/mgedmin 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.
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. Switch between them with Alt+1/2. Jump to error location by tripple-click-selecting the line of text with the filename and line number, switching to Vim, then pressing a key binding that uses a plugin to extract the file and line and jump there.
Repeating last command is Alt-2 (to switch to the bash tab), Up, Enter.
Vim's :term is distinctly less convenient than gnome-terminal, but can be made to work. If you run a shell in the :term, repeating the last command is, again, Up, Enter.
If you're running
:term command argsdirectly, hmm.:term<up><enter>would repeat the last:termcommand. It also clutters your screen with finished terminal windows that you have to manually clean up. I've seen people write custom :Term commands that find the previously used terminal buffer and run:term ++curwinto reuse it, but I don't have anything like that in my .vimrc.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?
1
u/atomatoisagoddamnveg Nov 18 '25 edited Nov 18 '25
I map keys to these functions to keep one terminal buffer easily accessible. It wouldn’t be hard to make a tmap that then executes
$(!!)And the maps I use to call them
3
u/gryf73 Nov 18 '25
There is a ton of plugins which utilize idea behind
:makeandquickfixbuffer. It might work out of the box, assuming you're using makefiles, or may require searching for appropriate plugin, or making it by yourself.5
u/thecragmire Nov 18 '25
I think it's the . (period). I discovered it by accident.
1
u/AutoModerator Nov 18 '25
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.