In this scenario enter image description here

I press C-x C-s to save the file being edited in upper window
C-x o to switch focus to lower window
Alt-p to recall previous command in M-x shell buffer
Enter to execute command
# the command takes 4-5 seconds to complete, without waiting
C-x o to switch back to editing file in upper window

If I end up doing this 30 times each hour, it can become tedious. Can emacs help automate this?

Someone (@db48x) suggested using Function keys, this is my keyboard dunno what they referred to: enter image description here


Yes, you can record a macro. F3 to start recording, then F4 when you're finished. Then F4 will replay the macro.

Of course, this macro will go away if you record another macro, or if you exit emacs. Since you use it so much, you'll probably want to save it as a function that you can call, and then bind that function to a key. This is all detailed in the Emacs manual at http://www.gnu.org/software/emacs/manual/html_node/emacs/Save-Keyboard-Macro.html.

Edit: You need a better keyboard! Ok, I'm kidding. You can also use C-x ( and C-x ). These are actually the more traditional key bindings from back when most computers didn't have function keys. (Keyboards used to vary a lot more between computers; go check out a picture of a space-cadet keyboard some time.)


One-click operation in current file, and no need to switch, switch, switch:

(defun bundle-exec-rails-runner-current-file ()
  (let ((file-name buffer-file-name))
    (when file-name
       "bundle-exec-rails-runner" "*bundle-exec-rails-runner*"
       (format "bundle exec rails runner %s"
               (expand-file-name file-name))))))

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.