Take the 2-minute tour ×

I'm using Emacs 23 with xterm and am trying to bring my Linux system into the 21st century by switching from a POSIX locale to en_US.utf8. Problem: when I use the Alt key on the keyboard in this locale, xterm sends a two-character sequence. I'd like to fix emacs so that it sees, e.g., Alt-f as meta-f rather than the two-character sequence "\303\246". I think this might be a job for an input coding, but from the emacs documentation I have not figured out how to write one.

As an alternative I would be willing to add octal '\303' to a keymap so that emacs would treat it as a prefix character to introduce a meta escape. The way I would prefer to do this would be to bind '\303' to a function that then takes the next key and adjusts it as a meta key. But I can't quite figure out that one either.

Worst case I guess I write a loop that binds the meta keys for 'a' through 'z' or something equally horrid.

I've messed around with the set-terminal-coding-system, but the crux of the matter is that in a UTF-8 locale, xterm uses the Alt key to send non-ascii characters—the Alt key no longer behaves like meta. So setting the terminal coding system to UTF-8 merely enables me to enter Latin characters with diacritical marks. Not the behavior I hoped for.

I found a really horrid answer that involves guessing what xterm is doing around the meta key. Suggestions for improvement are welcome—I'd like to find code that could do this job cleanly.

(And FYI, I'm not the only one with this problem.)

share|improve this question
have you tried to set it via set-terminal-coding-system? What returns M-: (terminal-coding-system)? If I understand correctly, the problem maybe that your Alt isn't translated into Meta - check the terminal settings –  Alex Ott Jan 25 at 21:19
@AlexOtt neither nil nor utf-8 works. I think the fault lies in xterm, not in emacs. This is really a workaround for weird stuff that xterm does. For example, using rxvt, emacs -nw "just works" with the Alt keys. –  Norman Ramsey Jan 25 at 22:18
add comment

1 Answer

Here's a truly horrible hack:

(unless (display-graphic-p)
  (defun make-meta-key ()
    (let ((e (read-event)))
      (if (numberp e)
          (let ((keys  (vector (event-convert-list (list 'meta
                                                        (+ (event-basic-type e) 64))))))
            (let ((result (key-binding keys)))
              (command-execute result)))
        (error "this can't happen"))))
  (global-set-key [?\M-C] 'make-meta-key))

This seems to work around what xterm is doing.

share|improve this answer
add comment

Your Answer


