8

I know how to bind a key to a command in tmux, but I want to create a custom command (that you can type with prefix key + colon). In this custom command, I want to execute a couple of other commands.

My idea is to have something like this:

no-side-status() {
    set status-left-length 0
    set status-right-length 0
}

side-status() {
    set status-left-length 50
    set status-right-length 150
}

So I can type :no-side-status to hide the left and right status bars, and type :side-status to restore the left and right status bars.

Is it possible to create such custom commands? If so how? If not, any other way to achieve what I want?

5
  • You can have your own bash functions if you like. Not sure about which status bars are you talking about.
    – MinusFour
    Commented Oct 17, 2014 at 15:16
  • @MinusFour The tmux status bars..
    – gitaarik
    Commented Oct 17, 2014 at 15:17
  • Try adding the function keyword to your functions and add them at the bottom of your ~/.bashrc. Then execute them like no-side-status() from your tmux session.
    – MinusFour
    Commented Oct 17, 2014 at 15:31
  • @MinusFour I understand I can make a bash function and talk to tmux through the tmux command, but I would like to make a tmux function that I can execute by typing prefix-key + : and then the command name. Because if I'm in a vim session or something, I don't want to have to go back to bash to execute this command.
    – gitaarik
    Commented Oct 17, 2014 at 15:40
  • When you are in Vim you can execute the same command with !. For example :!no-side-status(). If you are looking for the same command from Vim and Bash, then I'm not sure if it's possible.
    – MinusFour
    Commented Oct 17, 2014 at 15:46

7 Answers 7

7

Originally, tmux doesn't have any support for custom commands except for running external shell scripts.

There's a mod adding full-fledged scripting support to tmux: http://ershov.github.io/tmux/

It also allows to create user commands. For example, yours would look like:

proc no-side-status {} {
    set status-left-length 0
    set status-right-length 0
}

proc side-status {} {
    set status-left-length 50
    set status-right-length 150
}

To use from tmux command line just type C-b : and side-status or no-side-status.

To bind it to a key use bind C-p tcl side-status.

1
4

bind C-p run "/usr/bin/notify-send Foo"

3
  • How does this answer my question?
    – gitaarik
    Commented Jan 28, 2015 at 12:17
  • Partial answering, that you can bind shortcut to a external shell function. (that you can type with prefix key + colon)
    – guneysus
    Commented Jan 28, 2015 at 12:29
  • I'm not trying to bind an external shell function to a shortcut.
    – gitaarik
    Commented Jan 28, 2015 at 15:06
2

This is the full answer. You can have two tmux configuration. One contain

set status-left-length 0
set status-right-length 0
bind-key R source-file ~/.tmux.alternative.conf \; \
                    display-message "Alternative configuration loaded"

The other contain

set status-left-length 50
set status-right-length 15
bind-key R source-file ~/.tmux.conf \; \
                    display-message "Default configuration loaded"
2
  • I could try this approach, though I actually don't want to bind it to a keyboard shortcut but want to create a custom command.
    – gitaarik
    Commented Jan 28, 2015 at 15:09
  • Good luck. It seems difficult without hacking the tmux core. You should consider the answers above, binding a key to shell scripts/functions.
    – guneysus
    Commented Jan 28, 2015 at 22:05
2

The closest experience you can get right now is:

  1. create tmux shell script and put it in $PATH
  2. run the script inside tmux using the run-shell command (short: ru)

Example in tmux (say the script is named tm.xx)

c-b :ru tm.xx

So you are typing three extra characters - ru

1
  • Agreed that this is probably the best bet still almost 7 years after the question was asked. I was thinking :source tm.tmux for "pure tmux commands", but that can't handle the PATH. run-shell can. Commented Aug 18, 2021 at 22:35
1

Define a command alias by adding something like this to your .tmux.conf:

set -s command-alias[10] widevim='splitw -bvfp 80 "vim -O2"'
1
1

There is a relative new feature of tmux that offers what you are looking for here: command aliases. Command aliases are different from the short alias of each command (such as lsw for list-windows). Command aliases are defined with the command-alias server option. This is an array option where each entry has a number.

The default has a few settings for convenience and a few for backwards compatibility:

$ tmux show -s command-alias
command-alias[0] split-pane=split-window
command-alias[1] splitp=split-window
command-alias[2] "server-info=show-messages -JT"
command-alias[3] "info=show-messages -JT"
command-alias[4] "choose-window=choose-tree -w"
command-alias[5] "choose-session=choose-tree -s"

Taking command-alias[4] as an example, this means that the choose-window command is expanded to choose-tree -w.

A custom command alias is added by adding a new index to the array. Because the defaults start at index 0, it is best to use higher numbers for additional command aliases:

:set -s command-alias[100] 'sv=splitw -v'

This option makes sv the same as splitw -v:

:sv

Any subsequent flags or arguments given to the entered command are appended to the replaced command. This is the same as splitw -v -d:

:sv -d
1
  • Thanks for expanding on my answer! Commented Jun 13 at 23:13
0

tmux supports float windows now. You could popup a window by using tmux display-popup. And then in this popup window, run the script you want to use.

1
  • Could you please use full sentences and correct your grammar? Even with knowledge about the topic it is hard to grasp what you are writing. Commented Nov 24, 2022 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .