I'm using Emacs and M-xterm for a terminal. Since my typical workflow looks like this:

  • edit some code
  • C-x o to the terminal buffer (or C-x bterm[Enter] or something)
  • press Up key to use the last command
  • press Enter to run it
  • C-x o to go back

I want to bind all of these (except the first step... maybe) to one command, I believe Emacs is awesome enough to do that :-)

So, a command must:

  • go to the buffer with terminal (maybe it shouldn't change any windows at all, maybe it should split the window vertially (if it weren't split already) and use the right sid)
  • run a last command what've been run there
  • go back to the last buffer/part of the screen

Thank you! I'm not really used to the Emacs scripting system, and I hope someone will help me and someone else will be able to use the answer to improve his workflow, since I believe this is a pretty common one

Examples of commands:

python manage.py test
python manage.py test stats
python solve.py # for project-euler puzzles :-)

the first and the second runs over a ssh (in a terminal) sometimes (I like developing with vagrant)

I understand that it's easy to bind the first and the third ones, but the second changes too often - I'd just like to "run last command"

  • 1
    It sounds like you're switching windows to do some sort of code compilation. If so, you should investigate the commands built into the coding modes which do this already w/o a separate shell. Give a concrete example of what you're trying to do and you'll get a better response. – Doug Harris Mar 7 '11 at 23:18
  • not actually, I'll add examples – valya Mar 7 '11 at 23:19

Quick & dirty - define a macro:

C-x b term RET
C-x b RET

Now pressing F4 again will repeat the actions you've done between the last F3/F4 pair. You can do much more with macros, including having more than one macro, editing macros, saving macros in .emacs, etc. For more info look at EmacsWiki and Emacs manual.


Doesn't M-x compile allow one to do something similar? It allows you to type a custom command and run it, then repeat this the next time...

Well, probably there will be problems with interactive commands (that's why you would like to use a term), and perhaps also with a persistent reusable ssh-connection (but TRAMP could help here: cd REMOTELOCATION; your_cmd could work).

So, at least, you can look at compile's source code to learn how buffers are maanged for such a scenario, and how commands are executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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