3

Today I started using desktop-mode. It's nice to be able to save a desktop using the desktop-save command, and load one using the desktop-change-dir command.

Sometimes I do M-x cd to change the working directory in Emacs. I have three clients at work, with files pertaining to them stored in ~/client_foo/, ~/client_baa/, ~/client_baz/. What if I wanted Emacs to have a background color of black when cd'd in to the first clients directory, green for the second client, and brown for the third. Is there any module that can help me?

3
  • Are you using a particular shell within Emacs GUI, or are you just opening lots of different buffers and you want each buffer to have a special color?
    – lawlist
    Commented Dec 9, 2015 at 16:33
  • i have .rb files open with 4 window splits. all four window splits would have a particular color depending on the value of the cd or cwd command. Commented Dec 9, 2015 at 16:43
  • 2
    You can't do it by window, but you can do it by buffer with face-remap-add-relative by testing the default-directory -- i.e., shorten it to the root client directory and if it matches, set the color. You can use the find-file-hook to attach your color function. See these related threads: stackoverflow.com/a/28008006/2112489 and emacs.stackexchange.com/a/7283/2287
    – lawlist
    Commented Dec 9, 2015 at 17:03

1 Answer 1

3

I do something like this, but for my tabbar and mode line (screenshots). I use red for some projects and blue for some projects.

I don't know of an existing module that does this. Here's an adaptation of what I use, but to match your question.

(defun my/set-local-colors ()
  "Set colors based on current directory"
  (cond
   ((string-match "/client_foo/" default-directory)
    (face-remap-add-relative 'default :background "black"))
   ((string-match "/client_baa/" default-directory)
    (face-remap-add-relative 'default :background "green"))
   ((string-match "/client_baz/" default-directory)
    (face-remap-add-relative 'default :background "brown"))))

(cl-loop for buffer in (buffer-list) do
         (with-current-buffer buffer (my/set-local-colors)))
(add-hook 'find-file-hook #'my/set-local-colors)
(add-hook 'dired-mode-hook #'my/set-local-colors)
(add-hook 'temp-buffer-setup-hook #'my/set-local-colors)
1
  • This is excellent -- I often have buffers open with same file, but from two different directories / revisions. As I'm updating one of the buffers, I want to make sure I'm updating the correct one. So, I change background color of the "old" version to remind me which-is-which!
    – pbuck
    Commented May 4, 2023 at 15:26

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.