A Bash script that creates a new tmux session and starts a live server in the selected directory. Perfect for web development workflows.
- Features
- Prerequisites
- Installation
- WSL Setup
- Tmux Key Binding Setup
- Usage
- How It Works
- Configuration
- Troubleshooting
- Credits
- Creates a new tmux session with live-server running
- Interactive directory selection with fzf
- Supports custom ports
- Automatically opens the server in your default browser
- Can be triggered with tmux key binding
- WSL compatible with proper browser integration
Make sure you have the following installed:
- tmux
- live-server (npm package)
- fzf
- xdg-utils (for
xdg-opencommand) - For WSL users: wslu package (for
wslviewcommand)
- Clone this repository or download/copy the script
- Make the script executable:
chmod +x tmux-live.sh- Move the script to your preferred location, for example:
mv tmux-live.sh ~/.local/scripts/tmux-live.shIf you're using Windows Subsystem for Linux (WSL), you need to configure the browser environment variable to properly open URLs in your Windows browser:
-
Install wslu if not already installed: Installation Guide
-
Add the following line to your
~/.bashrcfile:
export BROWSER=wslview- Reload your bashrc:
source ~/.bashrcThis ensures that when the script tries to open the live server URL, it will use wslview to open it in your Windows default browser instead of trying to use a Linux browser.
Add this line to your ~/.tmux.conf (replace ä with desired keybind):
bind-key ä run-shell "~/path/to/script"After adding the line:
- Either restart tmux
- Or reload the configuration by pressing
prefix + r
There are several ways to use the script:
- Using tmux key binding:
- Press
prefix + ä(default tmux prefix is ctrl+b) - Select directory from the fzf popup
- Server starts on default port 5050
- Press
- Default mode - opens fzf directory picker and uses port 5050:
./tmux-live.sh- Custom port mode - specify port first, then select directory with fzf:
./tmux-live.sh 3000- Direct path mode - specify directory path and use default port 5050:
./tmux-live.sh /path/to/directory- When executed, the script either:
- Shows an fzf popup to select a directory from predefined paths (~/school, ~/github_repositories, ~/projects)
- Uses the provided directory path
- Creates a new tmux session with the directory name
- Starts live-server in the session on the specified port (default: 5050)
- Opens your default browser to the live-server URL
- Attaches to or switches to the new tmux session
The script looks for directories in these paths by default:
~/school~/github_repositories~/projectsTo change these paths, modify thefindcommand in the script.
selected="$(find ~/school ~/github_repositories/ ~/projects -mindepth 1 -maxdepth 1 -type d | fzf-tmux -p 50% --header='Open live server:')"If you're using WSL and the browser doesn't open automatically when running the script:
- Verify that
wslviewis installed and working:
wslview --help- Check that the
BROWSERenvironment variable is set:
echo $BROWSER- If the variable isn't set, add it to your
~/.bashrc:
echo 'export BROWSER=wslview' >> ~/.bashrc
source ~/.bashrcIf you're on a regular Linux system and the browser doesn't open:
- Verify that
xdg-openis installed and working:
xdg-open --help- Test opening a URL manually:
xdg-open http://localhost:5050This script is heavily influenced by tmux-sessionizer, a script by ThePrimeagen. The project adapts his session management approach while adding live-server functionality for web development.