I'm trying to get a lisp script to run some commands for me when I open a file of a specific filetype. I know that I'm working in the correct init file because if I remove the theme from it, emacs has no theme when I start it up.

This is the script I'm using which doesn't work (no errors or anything):

(defun my-project-hook (filename)
  (when (string= (file-name-extension filename) "ts")

(add-hook 'after-load-functions 'my-project-hook)
  • In this context, "load" means "load as lisp code". I think you want find-file-hook instead (note that these are not called with an argument ! use buffer-file-name instead). You may also like to use auto-mode-alist and define your own major mode.
    – YoungFrog
    Commented May 14, 2015 at 8:16
  • 3
    Actually, it looks like you want a mode hook.
    – Dan
    Commented May 14, 2015 at 12:51
  • You haven't hit the issue yet because your function doesn't run but when it does you know that the above is not valid lisp. The body of your when call is malformed. The two function calls cannot be surrounded with parenthesis like they are. Note the difference between your code and @sds's Commented May 14, 2015 at 13:43

2 Answers 2


In Emacs terminology, these are two different steps:

  • Associate files with the .ts extension with the major mode typescript-mode.
  • Run the function tss-setup-current-buffer when Typescript mode starts.

To choose which major mode to use for certain file names, add an entry to the variable auto-mode-alist. Put the following line in your init file:

(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode))

\.ts\' is a regular expression that matches file names with the .ts extension.

To run the function tss-setup-current-buffer whenever Typescript mode starts (I presume that you want to run it even for Typescript mode files that don't have the .ts extension), add it to the Typescript mode startup hook.

(add-hook 'typescript-mode-hook 'tss-setup-current-buffer)

Depending on how you installed typescript.el and tss.el, you may also need to declare that the functions typescript-mode and tss-setup-current-buffer have to be loaded from those files. This avoids having to load typescript.el and tss.el as soon as Emacs start: they will be loaded on demand, when you first open a .ts file or run typescript-mode explicitly.

(autoload 'typescript-mode "Major mode for typescript files" t)
(autoload 'tss-setup-current-buffer "Set up the current file for TSS" t)
  • I am trying to hook magit-status but it prevent me to file that I want to open
    – alper
    Commented Jul 5, 2020 at 23:46

What you are looking for is find-file-hook:

(add-hook 'find-file-hook 'my-project-hook)
(defun my-project-hook ()
  (when (string= (file-name-extension buffer-file-name) "ts")
  • 6
    While that will work, it should be pointed out that this is not the correct way to start up major modes when finding a file or to set up customizations for the major mode. The auto-mode-alist should be used to determine when to startup typescript-mode, and a typescript-mode-hook should be used to run tss-setup-current-buffer. Commented May 14, 2015 at 13:45
  • 3
    Using (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode)) and (add-hook 'typescript-mode-hook 'tss-setup-current-buffer) would be the normal way to do it. Commented May 14, 2015 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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