Sorry if this is a really noob question. I have recently gotten into Emacs with the Spacemacs distribution and I installed a plugin called "focus." I want to be able to have the command "focus-mode" run when I open a specific buffer type, but I don't know to do this. I have to do M-x and type in "focus-mode" and it's kind of inconvenient. I really don't know Emacs Lisp that well. Is there anything I can put in the user config to achieve this? Thanks in advance.


The usual mechanism is called a hook: a hook is a variable containing a list of functions, and when the hook is run, every function in the list is executed. Every major mode has an associated hook which is run when that mode is enabled. Finally, there is a variable called auto-mode-alist which associates a major mode with a file (usually using the suffix of the filename).

E.g. the auto-mode-alist probably already contains an entry associating the suffix ".py" to the major mode python-mode. That mode has a hook python-mode-hook which is run when python-mode is enabled in any buffer, and that runs all the functions in the hook. So all you have to do is add a function that enables the minor mode of your plugin to the hook - something like this should be added to your initialization file:

(add-to-hook 'python-mode-hook #'focus-mode)

Then when you visit a file foo.py, Emacs knows to enable python-mode on the file because it finds an entry like this: ("\\.py\\'" . python-mode) in auto-mode-alist. When python-mode is enabled, it runs its hook python-mode-hook which then calls the focus-mode function (which, in the case of minor modes, enables the mode).

I assumed that your specific file type here is a python file, but the mechanism is general and applicable to (almost?) all major modes.


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.