4

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
    Oct 17 '14 at 15:16
  • @MinusFour The tmux status bars..
    – gitaarik
    Oct 17 '14 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
    Oct 17 '14 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
    Oct 17 '14 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
    Oct 17 '14 at 15:46
3

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.

2

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

3
  • How does this answer my question?
    – gitaarik
    Jan 28 '15 at 12:17
  • Partial answering, that you can bind shortcut to a external shell function. (that you can type with prefix key + colon)
    – guneysus
    Jan 28 '15 at 12:29
  • I'm not trying to bind an external shell function to a shortcut.
    – gitaarik
    Jan 28 '15 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
    Jan 28 '15 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
    Jan 28 '15 at 22:05
1

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. Aug 18 at 22:35

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.