mle0312
mle0312

Reputation: 425

How to write a tmux script so that it automatically split windows and opens a set of files?

I am new to Tmux. I know that you can write script to automate Tmux, in case your computer shuts down. I can write the following:

#!/bin/bash

tmux new-session -d -s MY_SESSION_NAME
tmux split-window -h
vim <path to file1>

This only opens up a 1 single vim editor page for file1, not in tmux, and not in any tmux split. Is it possible automate the file opening like this?

Upvotes: 2

Views: 2921

Answers (1)

jeremysprofile
jeremysprofile

Reputation: 11425

Here's an example of the automation I have on my tmux session. Target a specific session/pane/window with <name>:<window>.<pane>, where window and panes are numbered, starting with 0. Send-keys is the way to send a command to a particular tmux pane/window. the -d causes tmux to start in detached mode so you can keep sending more commands to it before actually attaching.

tmuxstart() {
    tmux new-session -d -s sess >/dev/null
    tmux rename-window -t sess:0 'main'
    tmux splitw -v -p 10 -t sess:0.0
    tmux splitw -h -p 80 -t sess:0.1
    #required; otherwise pane numbering is bs
    tmux select-pane -t sess:0.0
    tmux splitw -h -p 5 -t sess:0.0
    tmux send-keys -t sess:0.2 'sudo htop' Enter
    tmux send-keys -t sess:0.1 'tmux clock -t sess:0.1' Enter
    tmux select-pane -t sess:0.0
    tmux new-window -t sess
    tmux rename-window -t sess:1 'second'
    tmux splitw -v -p 10 -t sess:1.0
    tmux splitw -h -p 80 -t sess:1.1
    tmux select-pane -t sess:1.0
    tmux splitw -h -p 5 -t sess:1.0
    tmux clock -t sess:1.1
    tmux new-window -t sess
    tmux rename-window -t sess:2 'scratch'
    tmux splitw -v -p 10 -t sess:2.0
    tmux select-pane -t sess:2.0
    tmux splitw -h -p 5 -t sess:2.0
    tmux clock -t sess:2.1
    tmux select-window -t sess:0.0
    tmux a -t sess
}

Upvotes: 6

Related Questions