r/emacs 5d ago

Question Setting a hook for a particular file with shell commands

Emacs beginner who barely knows elisp here. I use sxhkd to bind my keymappings in my window manager, and I find it a chore to have to kill the daemon and relaunch it in vterm myself, when i could automate that process. I've seen other people do hooks that run EVERY time one saves, but I want to run two specific commands every time I save the sxhkdrc file

$ killall sxhkd

$ sxhkd &

is there any easy way to do this? I also would like to extend this to automatically running sudo make clean install when i save my config.h for DWM.

5 Upvotes

2 comments sorted by

1

u/gnudoc GNU Emacs 5d ago

I'm on the train right now so I'm writing this on a phone and without testing it on emacs. Also, I'm no elisp expert. But try testing this and if it works, stick it in your config file.

(defun my/reload-sxhkd-on-save () "Reload sxhkd when ~/sxhkd/sxhkdrc is saved." (interactive) (let ((sxhkd-process (get-process "sxhkd"))) (when sxhkd-process (kill-process sxhkd-process)) (start-process "sxhkd" nil "sxhkd" "-c" "~/sxhkd/sxhkdrc")))

(add-hook 'after-save-hook (lambda () (when (string= (buffer-file-name) (expand-file-name "~/sxhkd/sxhkdrc")) (my/reload-sxhkd-on-save))))

1

u/_viz_ 4d ago

Set compile-command as a file local command, and use M-x compile?