Skip to content

Instantly share code, notes, and snippets.

@jyurek
Last active April 25, 2025 04:13
Show Gist options
  • Select an option

  • Save jyurek/7be666a88e06f68d45cf to your computer and use it in GitHub Desktop.

Select an option

Save jyurek/7be666a88e06f68d45cf to your computer and use it in GitHub Desktop.

Revisions

  1. Jon Yurek revised this gist May 30, 2014. 2 changed files with 2 additions and 9 deletions.
    9 changes: 1 addition & 8 deletions tm.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,11 @@
    #!/bin/sh
    tm() {
    if [ -z $1 ]; then
    target_session=$(cat /tmp/previous-tmux-session)
    tm $target_session
    tmux switch-client -l
    else
    if [ -z "$TMUX" ]; then
    tmux new-session -As $1
    else
    record_previous_session
    if ! tmux has-session -t $1 2>/dev/null; then
    TMUX= tmux new-session -ds $1
    fi
    @@ -16,9 +14,4 @@ tm() {
    fi
    }

    record_previous_session() {
    current_session=$(tmux list-sessions | grep '(attached)$' | cut -d: -f 1)
    echo $current_session > /tmp/previous-tmux-session
    }

    tm $1
    2 changes: 1 addition & 1 deletion tmux.conf
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    # Toggle between the last two sessions
    bind m run "tm"
    bind m switch-client -l
    bind M command-prompt -p 'switch session:' "run \"tm '%%'\""
  2. Jon Yurek revised this gist May 30, 2014. 2 changed files with 22 additions and 9 deletions.
    28 changes: 19 additions & 9 deletions tm.sh
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,24 @@
    #!/bin/sh
    tm() {
    if [ -z "$TMUX" ]; then
    # not in tmux
    tmux new-session -As $1
    if [ -z $1 ]; then
    target_session=$(cat /tmp/previous-tmux-session)
    tm $target_session
    else
    # inside tmux
    if [ tmux has-session $1 ]; then
    tmux switch-client -t $1
    if [ -z "$TMUX" ]; then
    tmux new-session -As $1
    else
    TMUX= tmux new-session -ds $1
    record_previous_session
    if ! tmux has-session -t $1 2>/dev/null; then
    TMUX= tmux new-session -ds $1
    fi
    tmux switch-client -t $1
    fi
    fi
    }
    fi
    }

    record_previous_session() {
    current_session=$(tmux list-sessions | grep '(attached)$' | cut -d: -f 1)
    echo $current_session > /tmp/previous-tmux-session
    }

    tm $1
    3 changes: 3 additions & 0 deletions tmux.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Toggle between the last two sessions
    bind m run "tm"
    bind M command-prompt -p 'switch session:' "run \"tm '%%'\""
  3. Jon Yurek revised this gist May 22, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. Jon Yurek created this gist May 22, 2014.
    14 changes: 14 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    tm() {
    if [ -z "$TMUX" ]; then
    # not in tmux
    tmux new-session -As $1
    else
    # inside tmux
    if [ tmux has-session $1 ]; then
    tmux switch-client -t $1
    else
    TMUX= tmux new-session -ds $1
    tmux switch-client -t $1
    fi
    fi
    }
    7 changes: 7 additions & 0 deletions gistfile2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # tmux session tab complete function
    _tmux_complete_session() {
    local IFS=$'\n'
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions | cut -f 1 -d ':')" -- "${cur}") )
    }
    complete -F _tmux_complete_session tm