Search code examples
emacselisp

How to auto execute commands after opening a file in GNU Emacs


How to do something like this: auto execute commands (split-window-right) (follow-mode) (visual-line-mode) after opening a file.


Solution

  • One way to do it is to write your own "open file" command:

    (defun my-find-file ()
      "Like `find-file', but splits screen and enables Follow Mode."
      (interactive)
      (call-interactively #'find-file)
      (follow-delete-other-windows-and-split)
      (visual-line-mode 1))
    

    You can bind it to C-x C-f:

    (global-set-key (kbd "C-x C-f") #'my-find-file)
    

    I've used follow-delete-other-windows-and-split rather than split-window-right and follow-mode and the latter doesn't work that well when a frame already contains multiple windows.

    Also, you might consider enabling visual-line-mode using other mechanisms like mode-specific hooks or global-visual-line-mode.