Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tmux conditional color setting

Tags:

tmux

Is it possible to set window color depends on the other window-option?

When a windows has synchronize-panes enabled, I don't want to accidentally press C-d, or all panes will be closed.

So what I'm trying to do is to change window color on statusline based on synchronize-panes: (the following config doesn't work, though)

bind-key S setw synchronize-panes \; \  # toggles the option
           set -w window-status-bg '#{?pane_synchronized,yellow,default}' \; \  # error: bad color
           set -w window-status-current-fg '#{?pane_synchronized,yellow,default}'  # error: bad color

The most possible solution I can thought of is to use if-shell, but I prefer not to fork a shell just to read option of itself, if possible.


EDIT: This if-shell solution works for me on tmux 2.7

My statusline cyan colored, if synchronize-panes is enabled, cyan becomes yellow.

bind-key S setw synchronize-panes \; \
           if-shell '[ #{pane_synchronized} -eq 1 ]' \
               'set -w window-status-style fg=black,bg=yellow ; set -w window-status-current-style fg=yellow,bg=black' \
               'set -w window-status-style fg=black,bg=cyan ; set -w window-status-current-style fg=cyan,bg=black'

EDIT: Problem solved, my setting is now changed to this:

bind-key S setw synchronize-panes

sync_ind_colour="#{?pane_synchronized,yellow,cyan}"
set -g window-status-format "#[fg=black,bg=${sync_ind_colour}][#I#{?#{!=:#W,},:,}#W]"
set -g window-status-current-format "#[fg=${sync_ind_colour},bg=black][#I#{?#{!=:#W,},:,}#W]"

Looks a little bit scary but it's still readable.

like image 838
Cychih Avatar asked Dec 06 '18 11:12

Cychih


People also ask

How do I change the color on tmux?

In Tmux, hit Control-b, then :set -g status-bg colour260 . (Tmux uses international "colour" vs American "color.") Also note that Tmux has history, to update the color do Control-b, then : to enter command mode, and hit up arrow to edit the last set color command.

How do I customize my tmux Statusline?

Step 1: How to Change the Status LineTo change the foreground color (text color) it's just #[fg=colour*] (Again replacing * with a number between 0 and 255). Adding %H:%M in the <options> string will show the current time in a 24 hour format without seconds (something like 15:38).

How do I change my status bar in tmux?

Similarly, you can use this command from within tmux as well by hitting Prefix +: and typing set status off in the command mode. This is shown in the screenshot below: As you can see, the status bar for tmux has been turned off. However, it can be brought back by using set status on command in the command mode.

Which of the following are always in a tmux status bar?

By default, the status line is enabled and one line in height (it may be disabled or made multiple lines with the status session option) and contains, from left-to-right: the name of the current session in square brackets; the window list; the title of the active pane in double quotes; and the time and date.


1 Answers

It shouldn't be necessary to use if-shell for this. You can use conditionals in format options, but not in styles. The following minimal configuration should do what you want.

# toggle pane synchronisation mode
bind-key S setw synchronize-panes

# Variables
sync_ind_colour="#{?pane_synchronized,yellow,cyan}"

# status format
setw -g window-status-format "#[fg=black,bg=${sync_ind_colour}]#I #W"
setw -g window-status-current-format "#[fg=${sync_ind_colour},bg=black][#I #W]"

Note that I set the text of the window status to #I #W (and [#I #W] for active) as an example, but that's irrelevant to the question.

It's also not necessary to use a variable (sync_ind_colour, synchronise indicator colour), but it's simpler than defining the same conditional in both the window-status-format and the window-status-current-format variables.

like image 82
Achilleas Avatar answered Sep 29 '22 10:09

Achilleas