0

I'm working on a shell script to automate the way I install linux on my computers. I'd like to incorporate some emacs customizations into this script.

For example, as soon as emacs is installed, I'd like the following set:

  • The init file should be in ~/.config/emacs/init.el.
  • menu-bar-mode and tool-bar-mode should both be set to nil
  • backup-by-copying, delete-old-versions, global-linum-mode, line-number-mode, and version-control should be set to t
  • kept-new-versions and kept-old-versions should both be set to 5
  • backup-directory-alist should be '(("." , "~/.cache/emacs-saves"))

Is there a way to automate this from a shell script?

I suppose that one way to do it would be to simply write these settings to the init file with something like

#!/bin/sh

mkdir -p ~/.config/emacs
cat > ~/.config/emacs/init.el <<EOF
'(menu-bar-mode nil)
'(tool-bar-mode nil)
'(kept-new-versions 5)
'(kept-old-versions 5)
'(backup-bycopying t)
'(delete-old-versions t)
'(global-linum-mode t)
'(line-number-mode t)
'(version-control t)
'(backup-directory-alist '(("." . "~/.cache/emacs-saves")))
EOF
2
  • It’s a text file. Why would you need or want special tools to create it?
    – db48x
    Commented Jun 5, 2021 at 21:30
  • @db48x Well, I want to make sure I'm doing things correctly. For one, it seems like there are lots of different paradigms for dealing with init.el. Will doing what I've suggested persistently set the init file as ~/.config/emacs/init.el? Also, I know that some people set these configurations within emacs with M-x customize-variable. Are there advantages to this approach? If so, is this method scriptable? If what I have written is considered best practice, than I'm obviously very happy! Commented Jun 5, 2021 at 21:33

1 Answer 1

1

You’re over–thinking it. It really is just a text file, and you don’t need to do anything fancy here.

Customize is nice primarily because it groups all the settings into categories, gives the user an easy way to search for the settings they are interested in, and makes editing the values of complex settings easier. There’s really nothing that forces you to use it, nor does it sound like there is much for you to gain by using it. It doesn’t have any API either, though you can script it in the usual ways. However, because customize writes your settings back to your init file, you could use it to set up one copy of Emacs the way you want and then copy that init file to your other machines.

So all you have to do is to create a text file with the contents that you want, using any tools that you like in the process.

I will point out, however, that in your question you put a single–quote in the front of every line, which will break them. Putting a single–quote in front of a parenthesis turns it into a list literal, but you want to actually execute these instructions rather than collect them into a list. Also, some of them are function that you are calling, but others are variables that you need to set. For the latter you should use setf:

(setf backup-by-copying t)

However, none of that has anything to do with how you put this file into place. You’ll know that you have done it correctly when Emacs starts up with the configuration that you want.

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.