0

Is it possible to run a keyboard macro in Emacs right after opening a file? For example, when I open a text file I want to get information in buffer Hello world.

1

Keyboard macro vs. Function

Keyboard macros are supposed to be fairly ephemeral things. If you want something permanent, you should write a Lisp function.

Hooks

Emacs has a lot of "hooks" - lists of functions run at certain point that you can customize for your needs.

"Opening a file" is done by find-file in Emacs.

Thus, M-x apropos RET find hook RET shows a list of hooks in the *Apropos* buffer, and you immediately see the one you need: find-file-hook.

So, all you need to do is (untested!)

(defun my-find-file-hook ()
  "Mention the newly visited file in the 'Hello World' buffer."
  (let ((fname buffer-file-name))
    (with-current-buffer "Hello World"
      (insert "just opened files " fname)
      (display-buffer (current-buffer)))))
(add-hook 'find-file-hook 'my-find-file-hook)

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.