How to Set tmux for Opening Specified Windows at Startup?
In Linux to open specific windows at startup, we can configure the tmux utility. We need to modify the tmux configuration file in the system which will allow us to create windows and also specify the layout. To make sure that windows open when tmux starts, we can include the special command at the end of the configuration which will ensure the proper opening of windows. In this article, we will see a practical demonstration of setting up tmux to open specified windows at startup.
What is Tmux?
Tmux, or “terminal multiplexer“, is a command-line tool that allows you to manage multiple terminal sessions within a single window. It enables you to split your terminal into panes, each displaying its command line, and create multiple windows with different tasks or programs. Tmux is particularly useful for system administrators and developers working on remote servers or managing complex projects. It provides a persistent environment, allowing you to detach from a session, log out, and later reattach to the same session, ensuring your work continues uninterrupted. With its features, Tmux enhances productivity by providing an efficient way to organize and navigate your command-line interface.
How to set Tmux to open specified Windows at Startup?
In this section, we will perform step-by-step execution of setting Temux to open specified WIndows at Startup. So follow the below steps and execute the commands to open Windows efficiently without encountering any issues.
Step 1: Open the Terminal
Firstly, we need to launch the terminal on our Ubuntu system, we can launch by using the Application Menu or by using the Keybaord Shortcult as “CTRL + ALT + T”.
Step 2: Install tmux
After the successful launching of the terminal, we need to install the tmux utility by using apt manager. Execute the below command in the terminal for successful installation of tmux.
sudo apt-get install tmux
Step 3: Create a tmux startup script
Then, create a new file for the tmux startup script, for example, tmux_startup.sh. We can use the touch command to create the script file.
touch tmux_startup.sh
Step 4: Edit the startup script
After creating the file, we need to open the newly created tmux startup script in a text editor, for instance, nano editor.
nano tmux_startup.sh
Step 5: Add window specifications to the startup script
Then we need to populate the startup script with the content from the provided tmux startup script, adjusting commands and window names as needed.
#!/bin/bash
# Start a new detached tmux session named GFG_Session
tmux new-session -d -s GFG_Session
# Create the first window named GFG_Overview with an 'echo' command
tmux send-keys -t GFG_Session "echo 'Welcome to w3wiki!'" C-m
# Split the first window horizontally and send a 'pwd' command to the new pane
tmux split-window -h -t GFG_Session "pwd" C-m
# Create a new window named GFG_Code with an 'ls' command
tmux new-window -t GFG_Session "ls" C-m
# Split the GFG_Code window vertically and send an 'echo' command to the new pane
tmux split-window -v -t GFG_Session "echo 'This is the code directory'" C-m
# Create a new window named GFG_Terminal with an 'ls' command
tmux new-window -t GFG_Session "ls" C-m
# Attach to the tmux session
tmux attach-session -t GFG_Session
Step 6: Save and exit
Save the changes made in the text editor (nano) and exit to finalize the tmux configuration. We can press Ctrl + X, then Y, then Enter to Save and Exit from Editor.
Step 7: Make the script executable
Now, by using the chmod command we need to make the script executable to run it.
chmod +x tmux_startup.sh
Step 8: Run the tmux startup script
Now, by using the below command we need to execute the script. This script will create a tmux session with four windows, each with specific commands.
./tmux_startup.sh
Step 9: View Other Windows
By using the keyboard shortcut Ctrl + b, we can navigate to opened panes in the terminal. We need to press the Ctrl + b shortcut along with the window number.
For example: Press Ctrl+b, then 0 to switch to the “GFG_Overview” window.
Press Ctrl+b, then 1 to switch to the “GFG_Code” window.
Conclusion
In conclusion, setting tmux to open specified windows at startup involves defining a script or configuration file to automate the creation of windows and panes with predefined commands. Utilizing commands like new-session, new-window, and send-keys, users can customize the layout and content of each window. This approach allows for a seamless and efficient setup, ensuring that desired windows are automatically opened when tmux is initiated, streamlining the workflow for a more organized and productive terminal environment.