reddit / emacs

Isn't there a better way to set UTF-8 ?

metacontent · 2022-02-02 17:47
In my init.el I have the following section to set all things to UTF-8, I'm not even sure what all of them do or if they even effect how I use Emacs. It seems more than just a little rediculous to have to set UTF-8 in so many ways in a single application. So I have two questions: 1. Isn't there an easier/better way to do this? 2. Did I miss any UTF-8 settings? (set-language-environment 'utf-8) (set-terminal-coding-system 'utf-8 (set-selection-coding-system 'utf-8) (set-keyboard-coding-system 'utf-8) (set-default-coding-systems 'utf-8) (prefer-coding-system 'utf-8) (setq locale-coding-system 'utf-8) (setq default-file-name-coding-system 'utf-8) (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING)) (setenv "LC_CTYPE" "en_US.UTF-8")
redblobgames · 2022-02-02 18:08
I have these so I've clearly missed some! ;-( (prefer-coding-system 'utf-8) (set-default-coding-systems 'utf-8) (set-selection-coding-system 'utf-8) (set-language-environment "UTF-8") (set-charset-priority 'unicode) (setq default-process-coding-system '(utf-8-unix . utf-8-unix))
eccp · 2022-02-02 19:19
Is is possible that some/all of those have a default value which is obtained from the environment? Maybe try some ideas from here: https://perlgeek.de/en/article/set-up-a-clean-utf8-environment
podtatranec · 2022-02-02 19:44
I don't have any of those set AFAIK. I just set the env vars in my environment to en_US.UTF-8 and rest is automatic isn't it? LC_ALL and LANG specifically
xtifr · 2022-02-02 19:51
I don't have *any* of those set, and Emacs uses utf-8 everywhere by default for *me*. The fact that you're doing a setenv for LC\_CTYPE suggests that your environment is quite messed up. You should never have to set *any* of the LC\_ environment variables. (And yes, you're missing a bunch of them!) If undefined, the system (and I mean the whole system, not just Emacs) will default to whatever LANG is set to. I suspect that if you export LANG=en\_US.UTF-8 *before starting Emacs,* you should be able to delete *all* those lines from your init file. Are you starting Emacs via systemd? That skips all your environment setup, which is why I stopped using it.
graduale · 2022-02-02 20:01
FWIW, here's [a bit](https://github.com/hlissner/doom-emacs/blob/fca8bd7f3fa697a91774c2ddedcd1a47cd7da01a/early-init.el#L70) from [Doom's `early-init`](https://github.com/hlissner/doom-emacs/blob/master/early-init.el): ````el ;; Contrary to what many Emacs users have in their configs, you don't need ;; more than this to make UTF-8 the default coding system: (set-language-environment "UTF-8") ````
flyin1501 · 2022-02-02 20:30
The easier way should be that none of this is needed. The most you have to do might be something like: (set-locale-environment "en_US.UTF-8") (The previous version of this comment did this on Windows, which [will break things](https://www.reddit.com/r/emacs/comments/siuvpu/isnt_there_a_better_way_to_set_utf8/hvergqr/). `(set-language-environment "UTF-8")` might be better instead; it's also what Doom Emacs uses.) Emacs will handle virtually all encodings properly by default; `set-locale-environment` alone is enough to tell Emacs you want new files to use UTF-8. (My environments are Arch Linux in Japanese (everything is UTF-8 with zero configuration) and Windows in Japanese) --- - (setq locale-coding-system 'utf-8) This should probably be done with `set-locale-environment`. (set-locale-environment "en_US.UTF-8") - (prefer-coding-system 'utf-8) This is already done by `set-locale-environment`. - (set-default-coding-systems 'utf-8) This is already done by `prefer-coding-system`, which is already called as part of `set-locale-environment`. - (setq default-file-name-coding-system 'utf-8) Docstring: > This variable is set/changed by the command `set-language-environment`. User should not set this variable manually, [...] `set-language-environment` is already called by `set-locale-environment`. - (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING)) This does nothing. Docstring: > The value nil is the same as the list (UTF8_STRING COMPOUND_TEXT STRING). - (set-terminal-coding-system 'utf-8) This sets your current "terminal" (output device)'s coding system. It has no effect on graphical terminals. You should let Emacs figure this out itself. This will either make no difference if your terminal is indeed in UTF-8, or it would (I guess) mess up the display if your terminal isn't. - (set-selection-coding-system 'utf-8) This sets the encoding for the X11 clipboard. If your clipboard is in UTF-8, Emacs would've figured it out; if it's not, this would make acs decode it incorrectly. Once decoded, it doesn't matter what encoding it was in. - (set-keyboard-coding-system 'utf-8) Similar to `set-terminal-coding-system`, you should let Emacs figure these ones out itself. - (setenv "LC_CTYPE" "en_US.UTF-8") You probably want `(setenv "LANG" "en_US.UTF-8")` instead. See https://stackoverflow.com/questions/30479607/explain-the-effects-of-export-lang-lc-ctype-and-lc-all for explanations on LANG, LC_ALL, and LC_CTYPE.
metacontent · 2022-02-02 23:18
> (set-locale-environment "en_US.UTF-8") That's very helpful, thank you for clearing this up for me.
redblobgames · 2022-02-03 04:13
Thank you!
eli-zaretskii · 2022-02-03 12:02
Setting your locale-environment to UTF-8 on MS-Windows is *not recommended*. You will see hard-to-interpret and hard-to-debug failures with these defaults, especially when invoking sub-processes with non-ASCII I/O and command-line arguments. MS-Windows still doesn't support UTF-8 as a first-class citizen.
flyin1501 · 2022-02-03 13:24
Oh dear. Thank you for the correction! I've amended the comment to hopefully not continue to recommend something broken.