reddit / emacs

Can't seem to get init file to work?

phlepper · 2022-12-10 07:07
EDIT: Solved! It turns out this was a problem with Emacs vs Emacsclient. Apparently Emacsclient does not handle the frame settings, so when I added other items to the init file, I believe those **were** being executed. But ultimately, I'm not using emacsclient any more. Hello, I'm new to emacs and I built v28.2 on my Pop! OS Linux instance. I'm trying to get my init file to work to set the initial window (frame) size. I've created an init.el file in my .emacs.d folder in home (i.e., \~/.emacs.d/init.el), but it doesn't seem to be executed. When I start emacs, it looks like this: [Figure 1. Initial Emacs Window \(roughly 900x900\)](https://preview.redd.it/zf3qt9t3r05a1.png?width=932&format=png&auto=webp&s=301441c16f63bb7fd2ab5deb46075a8e13a8b932) What I want is for it to look like this: [Figure 2. Resized Emacs Window \(roughly 2500x1100\)](https://preview.redd.it/pe0bxc9er05a1.png?width=2460&format=png&auto=webp&s=538f4df035ee97df572ff469fcf984ad14c9d6dd) But it always starts out as Figure 1 and the only way to get it to Figure 2 is if I open my init.el (right-click in Nautilus and select emacs). And here is a third view showing my .emacs.d (shown by C-x C-f of \~/.emacs.d), the contents of the init.el file and the value of my user-emacs-directory (which is \~/.emacs.d/): [Figure 3. .emacs.d folder, init.el file, and user-emacs-directory variable value.](https://preview.redd.it/hnvo64bis05a1.png?width=2010&format=png&auto=webp&s=e908f7250c6b9c9d2ed7ff1258e6fccc9eba1fb9) I've also tried renaming the init.el file as .emacs and putting it my home folder with the same result. What am I missing here? EDIT: responses based on replies: * user-init-file shows "\~/.emacs", which, as noted above, I tried along with .emacs.d/init.el without success * When normally opening emacs, my/set-initial-frame is not defined. If I open the .emacs file from Nautilus, it opens the frame correctly and my/set-initial-frame **is** defined. If I rename the .emacs file to "foo" and open that from Nautilus, it doesn't set the frame and my/set-initial-frame is not defined. * No errors in \*Messages\* buffer * There is no emacs folder in .config * initial-frame-alist is always 'nil' (when it works and when it doesn't) * Not sure if I'm running emacs server, but server-mode variable is 'nil' Note the original .emacs content I found [here](https://www.reddit.com/r/emacs/comments/9c0a4d/tip_setting_initial_frame_size_and_position/). Thanks!
MitchellMarquez42 · 2022-12-10 07:38
Your parentheses look balanced, nice job. When you open emacs the way that doesn't work (figure 1), try C-h f my/set-initial-frame to see if it's even defined. If not, the function isn't getting defined at all. Also check the Messages buffer - C-x b \*Messages\* for any errors about loading. Also, is there anything in ~/.config/emacs? (shouldn't override ~/.emacs.d, but just in case) Also also, what's the default value of `initial-frame-alist`? If there's dimensions in there you may want to edit early-init.el too
7890yuiop · 2022-12-10 08:11
Does `C-h v user-init-file` report the path you're expecting?
CorysInTheHouse69 · 2022-12-10 09:08
Are you using the emacs server? If so then it’s quite difficult to get this working
_g3rtsch_ · 2022-12-10 14:01
~/.emacs and ~/emacs.d/ are default locations see also https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html#Init-File If they do not work I guess you have missed something while you built the software.
phlepper · 2022-12-10 17:03
The value is ""\~/.emacs" even though I have the file as init.el in .emacs.d. However, I also tried moving the file to .emacs in home folder and I still get the same result (i.e., Figure 1).
phlepper · 2022-12-10 17:09
my/set-initial-frame is not defined (gives \[No match\]). Also, no errors in \*Messages\*. I also don't have an emacs folder under .config (or anything similar or obvious as an emacs init). The initial-frame-alist value is 'nil'. If I open the .emacs file itself in emacs: * my/set-initial-frame **is** defined * initial-frame-alist is still 'nil' Why would it execute .emacs only when I open that file via Nautilus? As a test, I renamed the .emacs file to foo and opened that (via Nautilus) and it opened the file, but didn't set the window size or define my/set-initial-frame. As for my parentheses, that's not me. I took this particular snippet from [here](https://www.reddit.com/r/emacs/comments/9c0a4d/tip_setting_initial_frame_size_and_position/). :) Thanks!
phlepper · 2022-12-10 17:12
I did a quick look online and saw there is a variable called 'server-mode'. If that is how to check, it is set to 'nil'.
MitchellMarquez42 · 2022-12-10 18:06
Try M-x server-force-delete then reopen
phlepper · 2022-12-10 18:11
I did this and I get "Connection file "/run/user/1000/emacs/server" deleted", but otherwise, it had no impact.
7890yuiop · 2022-12-10 22:51
If it says `~/.emacs` (which is the default) then either (a) you actually *do* have a `~/.emacs` file; or (b) it's not seeing the filename you expect to be taking precedence. Does `(find-file user-init-file)` open the `~/.emacs` file that you have created? Or does it perhaps open an empty file at another location? (I think your symptom sounds rather like `HOME` is not where you think it is, but that would be pretty unusual on a GNU/Linux system.)
phlepper · 2022-12-11 06:06
Okay, so I "solved" the problem as I was looking into what u/7890yuiop suggested in terms of opening the "user-init-file": As mentioned previously, when I right-click and open the init file with emacs, the init file would work correctly and the window frame would be as original Figure 2. In further testing, it appears opening **any** file from Nautilus with right-clicking worked correctly. I was testing this versus just opening emacs from my dash, *where I had pinned it*, which did not work correctly. It turns out that I have two short-cuts in my applications menu (both placed by "make install" when I built emacs). One is just "Emacs" while the other is "Emacs (Client)". Running from the dash was running "Emacs (Client)" which has an exec of: Exec=sh -c "if [ -n \\"\\$*\\" ]; then exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" \\"\\$@\\"; else exec emacsclient --alternate-editor= --create-frame; fi" placeholder %F which I assume causes it not to get the right init file? Or maybe that "--create-frame" in the else is actually causing the frame problem and it is loading the init file (user-init-file shows "/home/phlepper/.emacs")? I did add the following to the init (just to give it a definitive, non-frame change), and that works correctly when right-clicking a file or choosing "Emacs" from the application menu, but not when choosing "Emacs (Client)". (set-face-attribute 'default nil :height 150) **Okay, so new problem:** For the above issue, I thought, "I'll fix this, I just apparently pinned the wrong version initially". So: 1. Unpin Emacs from the dash 2. Run Emacs afresh from the Applications shortcut (Emacs, not Emacs Client) 3. Right-click the icon and pin the new version Nope! No dice. Pinning the new running Emacs (which found the init file and created the frames correctly), gives a message of "Emacs **(Client)** has been pinned to dash" and clicking on that shortcut never works correctly (original Figure 1). Is there some way around this?
7890yuiop · 2022-12-11 10:06
Emacs can optionally start, or be run as, a server. `emacs --daemon` is a common approach to doing this (although there are other options). `emacsclient` can then be run to create a new frame for the existing server. `emacsclient --alternate-editor=` (with an empty value) is a special-case option/value pairing which says "if there is no existing server, start one first, then connect to that". So that's what your command is doing. So the first time you run it, it will start a server. Every subsequent time you run it, it will merely connect to the existing server. Starting the server means running `emacs` -- at which point your init file is evaluated. **Note carefully that your init file is *not* evaluated by `emacsclient`** (although again, if running that causes a server to be started, the server will load your init file when it starts). Please test your init file by running `emacs` on the command line. That will start a *new* instance of Emacs each time, and hence evaluate your init file each time. You can also kill the current instance of emacs, including any associated server, using `M-x kill-emacs` (i.e. use `emacsclient` to connect to the server, and then `M-x kill-emacs` from that client to kill it). See the manual for more details about the client/server system.