0

Platform

Windows 10 with GNU Emacs 26.1 (build 1, x86_64-w64-mingw32).

Goal

To set the encoding of process buffers to UTF-8.

I would like to set the input and output encoding of process buffers, like shell or the R Console (iESS), to UTF-8. By default, the iESS buffer is encoded as iso-latin-1-dos for output and undecided-dos for input. If I ask Emacs:

M-S-: (process-coding-system (get-buffer-process (current-buffer)))

I get:

(iso-latin-1-dos . undecided-dos)

As a result, special characters are mangled in the iESS output (although not in the input):

> print("Ä")
[1] "Ã\204"
>

What I've tried

I can change the encoding by invoking

M-S-: (set-buffer-process-coding-system 'utf-8 'utf-8)

as described in this thread. The problem goes away, but I would like a permanent solution. So I tried to create hooks for several process buffers in my .emacs, like so:

; the hooks below should set the encoding of input to and output from a
; process buffer (like a shell, R console, or REPL) to UTF-8
; but somehow they don't.
(add-hook 'shell-mode-hook (lambda () (set-buffer-process-coding-system 'utf-8 'utf-8)))
(add-hook 'eshell-mode-hook (lambda () (set-buffer-process-coding-system 'utf-8 'utf-8)))
(add-hook 'iESS-mode-hook
          (lambda () (set-buffer-process-coding-system 'utf-8 'utf-8)))
(add-hook 'inferior-python-mode-hook
          (lambda () (set-buffer-process-coding-system 'utf-8 'utf-8)))

Upon restarting Emacs, however, these settings have no effect. There is no error message, but the iESS and shell buffers still have the "wrong" encoding.

Questions

Ho can I fix the code above so it changes the encoding of the respective process buffer upon mode change? What would be a smarter way to achieve this? Is there a way to set all process buffers to utf-8 encoding without relying on individual hooks?

1 Answer 1

2

Just set default-process-coding-system to '(utf-8 . utf-8). You can do this by adding this to your init file:

(setq default-process-coding-system '(utf-8 . utf-8))

This and other useful variables are described in chapter 33.10.5 Default Coding Systems of the Emacs Lisp manual. You may also be interested in the process-coding-system-alist variable.

You can open this and other manuals inside Emacs using C-h i.

1
  • Very helpful, thanks a lot! Let me wait a day or two before accepting.
    – henning
    Sep 24, 2021 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.