Juan G. Hurtado
Juan G. Hurtado

Reputation: 2137

tmux: Bind key to open command prompt with predefined command

I would like to bind a key to open a command prompt in my tmux session, but with a pre-defined command ready to be executed.

Example: pressing <prefix> + p should open the command prompt with: source-file ~/.tmux/ and the cursor at the end of the line, ready to write the name of the file to be sourced.

I know how to open the command prompt with a keybind, but not how to fill it with a predefined command.

Is there any way to achieve this?

Upvotes: 6

Views: 1948

Answers (1)

Juan G. Hurtado
Juan G. Hurtado

Reputation: 2137

Got it!

bind p command-prompt -I "source-file ~/.tmux/"

-I option was the answer.

From tmux manpage:

command-prompt [-I inputs] [-p prompts] [-t target-client] [template]

Open the command prompt in a client. This may be used from inside tmux to execute commands interactively.

If template is specified, it is used as the command. If present, -I is a comma-separated list of the initial text for each prompt. If -p is given, prompts is a comma-separated list of prompts which are displayed in order; otherwise a single prompt is displayed, constructed from template if it is present, or `:' if not.

Both inputs and prompts may contain the special character sequences supported by the status-left option.

Before the command is executed, the first occurrence of the string %%' and all occurrences of%1' are replaced by the response to the first prompt, the second %%' and all%2' are replaced with the response to the second prompt, and so on for further prompts. Up to nine prompt responses may be replaced Po %1' to%9' Pc .

Upvotes: 8

Related Questions