6

I have some commands in mind that I don't want to create keybinds for and would prefer to use command mode for them. For example, I want something like:

<C-a>:restart-guard

That I can have run a script to run some commands in my guard window

Is this possible?

3 Answers 3

8

You can't define user defined commands directly But you can always call a tmux script with so (shortest alias of source-file) or a program with ru (shortest alias of run-shell)

For so, you need to give the path to the command or to have the tmux server to start in the folder where your custom commands are

Here is a simple example, you put your restart-guard script in ~/.tmux/commands you start tmux using a scipt :

#!/bin/bash
cd ~/.tmux/commands
tmux

then inside tmux, do

<C-a>:so restart-guard

I am currently looking for a way to have the directory where you started tmux and not the ~/.tmux/commands directory when starting

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice! I'll be using this for sure
this trick doesn't seem to be working for me. (tmux 2.3 on ubuntu)
3

That is unfortunately not possible with tmux at this moment.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.