Matt Erickson
Matt Erickson

Reputation: 684

Unicode characters in emacs term-mode

I use ansi-term for my normal terminal sessions. I tend to use unicode characters in my prompt to do things like set the trailing character based on the type of source control I'm using.

I use the character "±" as my prompt for git repositories.

In Emacs' ansi-term, my prompt isn't rendered as unicode, and shows as "\302\261". Displaying the current coding system shows that it defaults to utf-8-unix for input to the process, but I get raw binary as the decoding output. I can hit C-c RET p to change the encoding and decoding coding systems. I'm drawing a blank as to how to set this automatically when I start a terminal? I've tried adding to term-mode-hook to set the buffer's coding system to no avail. I think I've found what I'm looking for in term.el, but I don't care to tweak the distribution elisp, and it appears the raw binary was added to fix a bug somewhere else.

EDIT: This was unclear originally. I'm having issues setting the default process coding system for ansi-term running under Cocoa-ized Emacs 23.3 on MacOS. Emacs itself isn't running in a terminal, my terminal is running in Emacs.

Upvotes: 11

Views: 6835

Answers (3)

Michael Markert
Michael Markert

Reputation: 4026

Try

(set-terminal-coding-system 'utf-8-unix)

That's C-x RET t not C-x RET p.


So C-x RET p helps? Unless C-h v default-process-coding-system is (utf-8-unix . utf-8-unix) try

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

Upvotes: 6

Matt Erickson
Matt Erickson

Reputation: 684

After getting a better understanding of term.el, the following works:

(defadvice ansi-term (after advise-ansi-term-coding-system)
    (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))
(ad-activate 'ansi-term)

Trying this with term-mode-hook is broken because in term.el, term-mode-hook is called before switching to the terminal buffer, so set-buffer-process-coding-system breaks due to the lack of a process associated with the buffer.

Upvotes: 8

David Goodlad
David Goodlad

Reputation: 271

The following worked for me:

(add-hook 'term-exec-hook
          (function
           (lambda ()
             (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))))

ansi-term seems to ignore the default-process-coding-system variable, so I had to set it buffer-locally after it executes my shell.

Upvotes: 17

Related Questions