4

I'd like to try and get to a similar setup that I have with N/Vim in OSX, whereby Truecolor support is enabled in iTerm2 (which supports 24bit colors) but disabled in the native Terminal app (which doesn't support it). This is simply for flexibility - sometimes I forget to use iTerm, and sometimes I'm on a machine that is using my dotfiles, but isn't using iTerm.

I've checked that my terminal is rendering the truecolor palette by running this script which outputs the following in Iterm2

iTerm2 colours

and as expected, outputs the following in Terminal.app:

OSX terminal.app colours

I'm running fish shell version 2.5, and $TERM is set to xterm-256color inside of iTerm2 and also echos this in the terminal. This means if I run tput colors it outputs 256, but from reading around on the web, I don't think it ever outputs anything higher than this though.

In Vim I achieve this with the following line of code in my .vimrc file:

if has("nvim") && terminal_program == "iTerm.app"
  " has true colour support and is nvim
  set termguicolors
  let $NVIM_TUI_ENABLE_TRUE_COLOR=1
  colorscheme base16-materia
elseif terminal_program == "iTerm.app"
  " has true colour support but is using std vim
  set termguicolors
  colorscheme NeoSolarized
else
  set termguicolors
  colorscheme solarized
endif

which lets me have a different colourscheme for nvim and distinguishes between iTerm app and the standard Terminal app.

I know that the latest version of emacs supports True color, so I installed this by running:

brew install emacs --HEAD --use-git-head --with-cocoa

emacs --version
GNU Emacs 26.0.50
... etc ...

and opening it in the cocoa windowed mode shows the additional colours when I run list-colors-display

Glorious 16 million colours in windowed mode

but doing the same in the terminal with emacs -nw only renders the standard 256, and also looks weird:

emacs in terminal with 256 colors

And running emacs with a non-standard TERM as the commit message mentions doesn't seem to work:

env TERM=xterm-24bit emacs -nw
emacs: Cannot open terminfo database file

The result is also the same with if I set $fish_term24bit to 1 in my fish.config via set -g fish_term24bit 1. So what am I missing to get the True color support working in iTerm2, before I even write the conditional code?

** edit 1 **

So further digging has thrown up the following links that could be useful:

both of which mentions requiring a custom setb24/setf24 terminfo capability to be present, as shown in the example and pasted again below.

@example
$ cat terminfo-24bit.src

# Use colon separators.
xterm-24bit|xterm with 24-bit direct color mode,
   use=xterm-256color,
   setb24=\E[48:2:%p1%@{65536@}%/%d:%p1%@{256@}%/%@{255@}%&%d:%p1%@{255@}%&%dm,
   setf24=\E[38:2:%p1%@{65536@}%/%d:%p1%@{256@}%/%@{255@}%&%d:%p1%@{255@}%&%dm,
# Use semicolon separators.
xterm-24bits|xterm with 24-bit direct color mode,
   use=xterm-256color,
   setb24=\E[48;2;%p1%@{65536@}%/%d;%p1%@{256@}%/%@{255@}%&%d;%p1%@{255@}%&%dm,
   setf24=\E[38;2;%p1%@{65536@}%/%d;%p1%@{256@}%/%@{255@}%&%d;%p1%@{255@}%&%dm,

$ tic -x -o ~/.terminfo terminfo-24bit.src

TERM=xterm-24bit emacs -nw
@end example

As directed, I put this into a file called terminfo-24bit.src and ran the specified tic command, which generated the directory .terminfo in my home dir. However, now when I run emacs I get colour sequences everywhere, which leads me to believe emacs has entered 24bit colour mode, but iTerm2 isn't interpreting these correctly...

colour codes everywhere

  • 1
    My non-expert suspicion is that iterm requires a different terminfo entry than XTerm. I've no idea what, though, and can't find anything much through searches. – Alan Third May 2 '17 at 9:58
  • Thanks @AlanThird, I'm not an expert on terminfo either (this is the first time I've even encountered it!) I found this though - iterm2.com/documentation-escape-codes.html - which details the color escape codes that iterm supports. Do you think it's the case that if I rewrite the setb24 or setf24 to use these codes, then emacs will use the new format to output the correct sequences that the iterm will pick up? I'm not sure what the current xterm format is though to translate it... – purpletonic May 9 '17 at 20:13
  • 1
    This is unrelated to your problem, but FYI I have found the semicolon variant xterm-24bits to be better supported than the colon variant xterm-24bit. IIRC Emacs within tmux doesn't work with the latter. – Basil May 10 '17 at 11:05
  • Thanks @Basil, just confirmed that to be the case, so good to know. Will stick with the semicolon variant. – purpletonic May 10 '17 at 19:46
  • Great question! – Steven Shaw Feb 5 '18 at 6:22
6

I believe that the @ symbols in your terminfo example are part of the Texinfo markup, rather than part of the terminfo code. Your terminfo entry should look like:

# Use colon separators.
xterm-24bit|xterm with 24-bit direct color mode,
   use=xterm-256color,
   setb24=\E[48:2:%p1%{65536}%/%d:%p1%{256}%/%{255}%&%d:%p1%{255}%&%dm,
   setf24=\E[38:2:%p1%{65536}%/%d:%p1%{256}%/%{255}%&%d:%p1%{255}%&%dm,
# Use semicolon separators.
xterm-24bits|xterm with 24-bit direct color mode,
   use=xterm-256color,
   setb24=\E[48;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%dm,
   setf24=\E[38;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%dm,
  • Thanks @Alan-third, you got there just before I did! I posted a separate question on Stack overflow regarding texinfo files and just received the answer pointing out the @ symbols not being part of it. Knew they looked suspicious. – purpletonic May 10 '17 at 19:41
  • Ah, I ended up looking through the terminfo man page and noticed that there's no mention of @ anywhere. Glad it's sorted now. – Alan Third May 10 '17 at 22:06
  • 1
    Compiling this file produces two files - xterm-24bit and xterm-24bits, but neither is visible by shell: zsh: can't find terminal definition for xterm-24bit error when running TERM=xterm-24bit. Any ideas why? OSX 13 here. – user3156459 Sep 4 '18 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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