Skip to content

Resuming codespace terminal sessions

Martin Cleaver edited this page Aug 3, 2025 · 1 revision

๐Ÿงต Persistent Terminal Sessions in GitHub Codespaces with Bash + tmux

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.


โœ… Features

  • Auto-starts tmux on terminal login
  • Re-attaches to the last session if it exists
  • Saves command history immediately
  • Keeps your session alive within the Codespace lifespan

๐Ÿ›  Step 1: Update ~/.bashrc

Add the following to your ~/.bashrc file:

```bash

Ensure shell history is saved immediately and across sessions

export HISTFILE=~/.bash_history export PROMPT_COMMAND='history -a' shopt -s histappend

Automatically start or reattach tmux session

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 tmux starts automatically and command history is saved instantly.


๐Ÿ›  Step 2: Install tmux (if needed)

Most Codespaces include tmux, but if itโ€™s missing:

```bash sudo apt update && sudo apt install -y tmux ```


๐Ÿ›  Step 3: (Optional) Improve Usability with ~/.tmux.conf

Create or update the ~/.tmux.conf file:

```bash

Enable mouse and vi-style copy mode

set -g mouse on setw -g mode-keys vi set -g history-limit 10000

Reload config with Ctrl+b then r

bind r source-file ~/.tmux.conf ; display-message "Config reloaded" ```


๐Ÿ” How to Use

  • On opening or resuming your Codespace:

    • Youโ€™ll automatically be in a persistent tmux session.
    • If you split panes, run tasks, or navigate between windows, it all gets saved.
  • Use Ctrl+b shortcuts to manage panes and windows:

    • % โ†’ split pane vertically
    • " โ†’ split pane horizontally
    • d โ†’ detach from session
    • tmux attach โ†’ reattach manually if needed

๐Ÿ’ก Tips

  • 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 postCreateCommand if you'd like it automated.


๐Ÿ“ฆ Bonus: Dev Container Integration

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!