Terminal emacs colors only work with TERM=xterm-256color

前端 未结 6 1410
攒了一身酷
攒了一身酷 2020-12-07 16:12

I\'ve found that terminal emacs does not render the correct colors unless I explicitly set TERM=xterm-256color. I use gnome-terminal, and from what I understand, TERM should

相关标签:
6条回答
  • 2020-12-07 16:16

    I am not that familiar with how emacs handles different terminals exactly. But looking at lisp/term directory in emacs sources, I found out that the existence of a function terminal-init-xxx allows you to add support for different terminals. For example, I've got:

    (defun terminal-init-screen ()
      "Terminal initialization function for screen."
       ;; Use the xterm color initialization code.
       (xterm-register-default-colors)
       (tty-set-up-initial-frame-faces))
    

    in my .emacs, which adds support for screen-256color. You may try defining a similar function for gnome by renaming the above function to terminal-init-gnome.

    NOTE: If you are interested, you can try to track down the calls from tty-run-terminal-initialization code. It first gets the terminal type using tty-type function, then looks at certain locations to load a relevant terminal file, then tries to locate the matching terminal-init-xxx function, and finally calls it. It may help you figure out the correct name for gnome-terminal.


    It looks like unless your TERM indicates that your terminal has 256 colors, emacs will only use 8. Changing TERM to gnome-256color allowed the color registration functions to work.


    There is a way to cheat, after all. When I run gnome-terminal, my terminal is set to xterm by default. Instead of changing TERM variable, it is possible to redirect xterm to another terminal, say, gnome-256color. Simply create the directory $(HOME)/.terminfo/x, then run ln -s /usr/share/terminfo/g/gnome-256color ~/.terminfo/x/xterm. I think this is better than setting TERM manually in .bashrc, because it only redirects a particular terminal to something else. A console login would still leave TERM as linux, and not xterm-256color.

    0 讨论(0)
  • 2020-12-07 16:22

    On ubuntu 10.04 I too had noticed that running emacs -nw inside byobu/tmux/screen was using different colours from emacs -nw in the regular gnome-terminal.

    I found that this is because byobu was setting TERM to screen-bce. Then setting TERM to xterm (for me, in the normal gnome-terminal TERM=xterm) gave me the same syntax highlighting when not running through byobu/screen.

    So still not sure what the proper solution is.

    See also this post: Emacs Python-mode syntax highlighting

    0 讨论(0)
  • 2020-12-07 16:26

    Terminals are a special type of device. When a process sends special byte sequences (called control sequences) to the terminal, it performs some action (like cursor positioning, change colors, etc).

    You can read the ANSI terminal codes to find more detail about control sequences.

    But terminals come from 70s, when hardware was limited in its capabilities, and a terminal cannot provide info about its capabilities (ie. which sequences it supports).

    $TERM was used to resolve this issue - it allows programs to know what to send to the terminal to get the job done. termcap and terminfo are databases that store info about terminal capabilities for many $TERM names. If your $TERM is not in the db, you must ask an administrator to add it.

    All terminal emulators inherit these limitations from old hardware terminals. So they need a properly set $TERM, and the terminfo/termcap DB MUST have data for this terminal. When a virtual terminal starts it sets the $TERM variable for you (and inside programs like bash). If $TERM is not in the terminfo/termcap you can quickly define an alias from $TERM to xterm-256color (you can find examples in the termcap file on how to do that).

    0 讨论(0)
  • 2020-12-07 16:30

    Maybe I'm not understanding something, buy why don't you run emacs like this:

    TERM=xterm-256color emacs -nw
    

    This way Emacs has its own TERM setting that you know works. You can also make an alias or wrap this in shell-script.

    0 讨论(0)
  • 2020-12-07 16:30

    Add this to your ~/.emacs:

    (add-to-list 'term-file-aliases
        '("st-256color" . "xterm-256color"))
    

    It tells emacs that if it sees TERM=st-256color then it should initialize the terminal as if it had seen TERM=xterm-256color.


    Longer answer:

    Emacs is showing strange colors because it thinks your terminal can only support 8 colors. In Emacs, run M-x list-colors-display to see the colors it thinks are available. The correct number of colors is detected during terminal-specific initialization. It says, in part:

    Each terminal type can have its own Lisp library that Emacs loads when run on that type of terminal.

    On my machine, the terminal-specific initialization files are in /usr/local/share/emacs/25.*/lisp/term. It has files for xterm, rxvt, screen, etc. but nothing for st. We need to help Emacs find the right initialization file. The documentation further says:

    If there is an entry matching TERM in the term-file-aliases association list, Emacs uses the associated value in place of TERM

    So that association list is a recommended way to handle unknown terminals. It works without you having to manually override the TERM environment variable.

    0 讨论(0)
  • 2020-12-07 16:33

    This behavior has to do with the logic EMACS uses to determine whether the terminal background is dark or light. Run M-x list-colors-display with TERM set to either xterm-256color or screen-256color and you'll see that the exact same colors are listed. As you pointed out in the comments, the difference in color schemes that you've observed is due to the frame background mode. To see this, with your TERM set to screen-256color, compare the colors in

    emacs -Q -nw --eval "(setq frame-background-mode 'light)"
    

    and

    emacs -Q -nw --eval "(setq frame-background-mode 'dark)"
    

    The function frame-set-background-mode (in frame.el) checks to see whether the terminal type matches "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)" if it can't deduce the background color otherwise.

    Within a running session, you can change the color scheme to 'light by evaluating

    (let ((frame-background-mode 'light)) (frame-set-background-mode nil))
    
    0 讨论(0)
提交回复
热议问题