13
votes

tmux has a command mode that can be accessed via C-b : and I'm wondering if there is a way to alias commands in my .tmux.conf file like split-window to something I use more often like vsp in vim.

I know I can bind keyboard shortcuts with bind but can I alias commands as well?

5

5 Answers

6
votes

tmux 2.4 adds the command-alias array option which does this, so for example you can do

:set -s command-alias[10] vsp='split-window -h'

And then you can use :vsp in the command-prompt just like you'd expect.

4
votes

This doesn't appear to be possible as of tmux 2.0.

One thing you can do, however, is send commands to the enclosing tmux session from the shell. This means that you can, for example, create a bash alias that can split windows:

alias vsp="tmux split-window -h"

You can then run vsp from your bash prompt to split the tmux window vertically. Depending on your specific use case, this might help.

It's also worth noting that, if minimising typing is the goal, tmux commands can be shortened to their shortest unambiguous prefix: sp does the same thing as split-window.

2
votes

You can use bind. Here is an example alias for turning mouse-mode on and off:

bind m \
    set -g mode-mouse on \;\
    set -g mouse-resize-pane on \;\
    set -g mouse-select-pane on \;\
    set -g mouse-select-window on \;\
    display 'Mouse mode ON'

bind M \
    set -g mode-mouse off \;\
    set -g mouse-resize-pane off \;\
    set -g mouse-select-pane off \;\
    set -g mouse-select-window off \;\
    display 'Mouse mode OFF'

Now you can easily use Ctrl+B m to turn it on, and Ctrl+B M to turn it off (assuming Ctrl+B is your prefix)

1
votes

There's a mod allowing not only alias but also create new commands in tmux: http://ershov.github.io/tmux/

For example:

proc vsp {args} { split-window -h {*}$args }

No external shell involved, no new processes spawned.

0
votes

Yep. Using bind-key in you tmux.conf. For example to split tmux windows use:

bind-key v split-window -v #C-b v to split vertically
bind-key h split-window -h #C-b h horizontal

If you don't want to use prefix (C-b) just add -n param:

bind-key -n C-right next # C - → to move to next window.