30

When I search in, for example, man ls while in a tmux session, the search strings don't appear highlighted - the page jumps down so that the search string is on the top line of the buffer, as expected, but it's not highlighted.

Doing the same thing in the same shell while not in a tmux session results in highlighted search strings.

I have no idea where to start looking to solve this. Any hints are appreciated.

Richard Hansen
  • 44,218
  • 20
  • 84
  • 95
simont
  • 57,012
  • 16
  • 110
  • 130
  • The most likely explanation is that tmux sets the `TERM` environment variable to `screen`. For some reason the `screen`-type terminal does not support highlighting on your system. I just tried on my old Ubuntu box with tmux 1.6 and PuTTY, and at least this combination works properly. – Casper May 12 '12 at 07:33
  • Just for kicks you could try running tmux with `tmux -2` and see if that fixes it. – Casper May 12 '12 at 07:36
  • @Casper I get highlighted strings in `screen` sessions, where `$TERM=screen`, though. Running `tmux -2` or `tmux -8` doesn't appear to help. – simont May 12 '12 at 10:36
  • Yeah. Must be something in tmux. I found a machine where I can kind of semi-reproduce your problem. When I run tmux on this machine all color capabilities disappear. But like for you running screen works on this machine too, but not tmux. – Casper May 12 '12 at 11:09

3 Answers3

20

Based on Less Colors For Man Pages by Gen2ly, here is my man page and how to do it:

Preview

This is a shell, not a web page ! Custom search highlighting in man page

How to

  1. (optional) I'm using Tomorrow theme for Konsole/Yakuake ;
  2. Edit your ~/.bashrc ~/.zshrc, etc. to add :

    # Colored man pages: http://linuxtidbits.wordpress.com/2009/03/23/less-colors-for-man-pages/
    # Less Colors for Man Pages
    export LESS_TERMCAP_mb=$'\E[01;31m'       # begin blinking
    export LESS_TERMCAP_md=$'\E[01;38;5;74m'  # begin bold
    export LESS_TERMCAP_me=$'\E[0m'           # end mode
    export LESS_TERMCAP_se=$'\E[0m'           # end standout-mode
    export LESS_TERMCAP_so=$'\E[38;5;016m\E[48;5;220m'    # begin standout-mode - info box
    export LESS_TERMCAP_ue=$'\E[0m'           # end underline
    export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
    
  3. Reload your config and try a man page search :

    . ~/.bashrc && man ls
    
Édouard Lopez
  • 32,718
  • 23
  • 108
  • 161
  • What does `$'\E[01;31m'` mean? I am trying to figure out how to set this env var in a csh/tcsh shell. – Kaushal Modi Sep 07 '16 at 14:22
  • I was able to gets the colors in `man` using `less` on `tcsh` using [this solution](http://unix.stackexchange.com/a/147/57923).. using `tput` instead of escape codes. For `tcsh`, replace `$(foo)` with foo. – Kaushal Modi Sep 07 '16 at 14:55
  • I would still like to learn how `export LESS_TERMCAP_mb=$'\E[01;31m'` translates to the `setenv` command in `tcsh`.. the `$` char in there is confusing. – Kaushal Modi Sep 07 '16 at 15:16
  • @KaushalModi `$'…'` is an [ANSI C-like string](http://wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings) – Édouard Lopez Sep 07 '16 at 15:53
  • Thanks. Google fails me in finding an equivalent way to represent ANSI strings in tcsh. I am glad the `tput` method exists :) – Kaushal Modi Sep 07 '16 at 16:15
  • @KaushalModi apparently there is no such thing: http://hyperpolyglot.org/unix-shells#quoting – Édouard Lopez Sep 08 '16 at 08:26
  • Thanks for doing some research on this. That's what I derived too. Before anyone asks why I am using tcsh, I am not doing it by choice :) – Kaushal Modi Sep 08 '16 at 10:38
19

Fixed it. The problem is to do with the way that the screen $TERM handles italics. From the tmux FAQ:

vim displays reverse video instead of italics, while less displays italics (or just regular text) instead of reverse. What's wrong?

This matches my problem exactly. The $PAGER used by man is less by default - basically, man uses less to show the contents of the manual pages. In my case, less wasn't highlighting text, just showing regular text.

The reason for this happening:

Screen's terminfo description lacks italics mode and has standout mode in its place, but using the same escape sequence that urxvt uses for italics. This means applications (like vim) looking for italics will not find it and might turn to reverse in its place, while applications (like less) asking for standout will end up with italics instead of reverse.

The solution is to make a new terminfo file for tmux, which lets it know that italics are supported. The solution's outlined in the (at time of writing) very, very bottom of the tmux FAQ.

After creating the new terminfo file, in tmux: C-b :source-file /absolute/path/to/.tmux.conf (from this SuperUser question) - this should make tmux reload the .tmux.conf file. However, this didn't work for me, and the changes only applied after restarting the tmux server (close all tmux sessions, then re-open them).

Community
  • 1
  • 1
simont
  • 57,012
  • 16
  • 110
  • 130
  • 2
    Regarding applying the settings to a running `tmux` instance: it reloads and applies the configuration just okay, the problem is that clients (programs) already running in windows and panes of that `tmux` instance initialize their terminal settings from the `TERM` environment variable they've read at the time they were starting up, and while newly started clients will see the new terminal name in their environment, those which are already running have no way to get it and their settings updated -- short of re-running them (and the shells they were started from, if any). – kostix Mar 16 '15 at 18:00
  • 1
    On Debian 8 Jessie, the original FAQ suggestion doesn't work. Symptoms: `terminal 'tmux-256color': resolution of use=xterm+256setaf failed`. In this case @Christy's solution did the trick. – sphakka Jun 18 '16 at 15:51
13

This thread is a few years old but is still the one that comes up as the best search result, so I'm answering with what finally worked for me. This is based off of tmux FAQ.

...but the instructions aren't completely clear on when or where to substitute the -256color string. I use gnome-terminal (v 3.16.2) with tmux, and this worked for me:

$ mkdir $HOME/.terminfo/
$ screen_terminfo="screen-256color"
$ infocmp "$screen_terminfo" | sed \
  -e 's/^screen[^|]*|[^,]*,/screen-256color|screen with italics support,/' \
  -e 's/%?%p1%t;3%/%?%p1%t;7%/' \
  -e 's/smso=[^,]*,/smso=\\E[7m,/' \
  -e 's/rmso=[^,]*,/rmso=\\E[27m,/' \
  -e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo
$ tic /tmp/screen.terminfo

And tell tmux to use it in ~/.tmux.conf:

set -g default-terminal "screen-256color"

Note: I tried it once without the -256color and since that didn't work (still seeing italics instead of highlighting), I had to delete everything under the .terminfo dir (another dir called 's') before the infocmp would work.

Kaushal Modi
  • 990
  • 2
  • 13
  • 40
Christy
  • 219
  • 2
  • 7
  • 1
    I was still seeing italics instead of standout/highlighting when viewing logs with `less`, which I realized was b/c I was using sudo (since they're under /var/log). To fix: ` ~> sudo rsync -rP ~/.terminfo /root/. sending incremental file list .terminfo/ .terminfo/s/ .terminfo/s/screen-256color 1,560 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/3) ~> sudo cp ~/.tmux.conf /root/. ` – Christy Jul 29 '15 at 15:48
  • 2
    Works for me. I created [this one liner](https://gist.github.com/stuartcarnie/818d14647cd1a42a4fbbcd689675b8c0) so I can add it quickly to new servers – Stuart Carnie Apr 07 '17 at 23:08