-
Notifications
You must be signed in to change notification settings - Fork 9
Resuming codespace terminal sessions
This guide sets up a reliable, persistent terminal environment using tmux and bash inside GitHub Codespaces. It ensures your terminal sessions and history survive Codespace suspensions and resumes.
- Auto-starts
tmuxon terminal login - Re-attaches to the last session if it exists
- Saves command history immediately
- Keeps your session alive within the Codespace lifespan
Add the following to your ~/.bashrc file:
```bash
export HISTFILE=~/.bash_history export PROMPT_COMMAND='history -a' shopt -s histappend
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then TMUX_SESSION_NAME="main" if tmux has-session -t "$TMUX_SESSION_NAME" 2>/dev/null; then exec tmux attach-session -t "$TMUX_SESSION_NAME" else exec tmux new-session -s "$TMUX_SESSION_NAME" fi fi ```
๐ก This ensures
tmuxstarts automatically and command history is saved instantly.
Most Codespaces include tmux, but if itโs missing:
```bash sudo apt update && sudo apt install -y tmux ```
Create or update the ~/.tmux.conf file:
```bash
set -g mouse on setw -g mode-keys vi set -g history-limit 10000
bind r source-file ~/.tmux.conf ; display-message "Config reloaded" ```
-
On opening or resuming your Codespace:
- Youโll automatically be in a persistent
tmuxsession. - If you split panes, run tasks, or navigate between windows, it all gets saved.
- Youโll automatically be in a persistent
-
Use
Ctrl+bshortcuts to manage panes and windows:-
%โ split pane vertically -
"โ split pane horizontally -
dโ detach from session -
tmux attachโ reattach manually if needed
-
-
To save history manually:
```bash history > session-commands.txt ```
-
To log everything:
```bash script session.log ```
-
You can also add this setup in your devcontainerโs
postCreateCommandif you'd like it automated.
If you want this pre-installed, include the .bashrc edits in your Dockerfile or mount a dotfiles repo.
Let me know if you want help automating this in your .devcontainer setup!