1. 35
  1.  

  2. 8

    I do something quite similar except I break my session up by path. I rely on a script called tat from the wonderful folks at ThoughtBot.

    I have this in my .zshrc file so that when I open a terminal, I’m kicked into a tmux session automatically:

    # Add this to your zshrc or bzshrc file
    _not_inside_tmux() { [[ -z "$TMUX" ]] }
    
    ensure_tmux_is_running() {
      if _not_inside_tmux; then
        tat
      fi
    }
    
    ensure_tmux_is_running
    

    And then when I navigate to a folder where I want a new session, I hit Ctrl-B which is defined in my .tmux.conf file to call tat again.

    bind-key C-b send-keys "tat" "C-m"
    

    This setup keeps me always in tmux but with sessions based on each project that I work on.

    Edit to add: I forgot the most useful part of this! I have a tmux shortcut of C-j that opens a split showing me the other sessions. It uses fzf so that I can fuzzy match and switch among the sessions:

    # Fuzzy matching session navigation via fzf utility
    bind C-j split-window -v "tmux list-sessions | sed -E 's/:.*$//' | grep -v \"^$(tmux display-message -p '#S')\$\" | fzf --reverse | xargs tmux switch-client -t"
    
    1. 1

      Brillant, I’m definitively stealing this.

      1. 1

        I’ve had something similar for years on all my remote shells, automatically throw me into a tmux.

        For some reason I stopped doing that, not even sure why. I think it misfired and I had to do “ssh host mv .zshrc” one time too often, so now I start it manually again.

      2. 5

        I like this a lot! I use fish so I translated the script from the post to fish syntax (with some modifications for personal opinion)

        function tmux_chooser
          if test "$TMUX" != ""
            return
          end
        
          set session_count (tmux list-sessions | wc -l)
          set output_names (tmux list-sessions -F\#S)
          set i 1
          echo "Choose the session to attach: "
          for session in $output_names
            echo "  $i - $session"
            set i (math $i + 1)
          end
          echo "Or create a new session by entering a name for it"
          read -P '> ' input
          if test "$input" = ""
            tmux
          else if test "$input" = "nil"
            return
          else if begin ;
                  string match -r '^[0-9]+$' "$input" > /dev/null ;
                  and test "$input" -le "$session_count" ; end
            tmux a -t "$output_names[$input]"
          else
            tmux new -s "$input"
          end
        end
        
        
        1. 3

          I pretty much live in Emacs, and I’ve primarily used tmux/screen as a means of leaving processes running on a remote machine even if I’m disconnected. (If nohup is not sufficent.)

          I’m getting the impression you use for development locally? Can you talk more about what you “split”? I don’t understand why you’d terminate the program to re-launch in tmux, rather than simply launching another terminal. (Or using C-z & bg.)

          1. 2

            I’m getting the impression you use for development locally?

            Yes, I use tmux mostly for local development.

            Can you talk more about what you “split”?

            Often running commands, firing up a seperate project for reference in a different vim session, looking at outputs of various commands, etc.

            I don’t understand why you’d terminate the program to re-launch in tmux, rather than simply launching another terminal.

            Usually, tmux is far more convenient and faster than managing multiple terminal windows even with tiling managers.

          2. 3

            I also do something similar. I changed my keybinding to launch my terminal from “launch terminal” to “launch terminal with tmux.” That way, whenever I open a terminal, I’m automatically in tmux.

            I don’t use named sessions too much. Mostly only when working remotely, in which case, I just attach to a session named remote. Otherwise, all of my sessions are just increasing integers. To make sure those sessions don’t pile up, I have a hook installed on session create/close that cleans things up and renumbers them.

            I actually started this when I switched to alacritty because, at the time, it didn’t support scrollback. Alacritty supports that now, but I liked the tmux setup so much that I just stuck to it. Namely, I like that it permits keyboard driven vim-style visual selections. I also like that, since everything is in a tmux session, if I leave my workstation and later connect to it via SSH, I can jump into any of the sessions I may have left open. Really handy.

            1. 1

              I don’t use named sessions too much. Mostly only when working remotely, in which case, I just attach to a session named remote.

              The terminals that I use, Apple Terminal, Windows Terminal, and Konsole, all give me an environment variable with a UUID in it. Only Apple Terminal does this really nicely and preserves that across terminal restarts (I believe this is planned for Windows Terminal), but even without that it’s very useful. I use that to set the tmux / abduco session ID in every remote session and autossh to protect the session, so when I move between networks or send my laptop to sleep it can reconnect all of my remote sessions and everything works.

              With the Apple Terminal, all of my remote terminals persist across reboots as well.

            2. 3

              You do always have the option of making tmux your login shell – as tmux understands this. It’s what I’ve done for years and that way I’m never not in tmux. Until it crashes, but that’s very rare these days.

              1. 3

                I’ve never worked out how to get tmux to play nicely with terminal scrollback so I end up avoiding running it when I don’t need it. I’ve started using dtach for the keep-running-when-ssh-goes-down features paired with some trickery to automatically connect the right terminal tabs to the right dtach sessions and that works quite nicely thank you very much.

                1. 1

                  The tmux team decided that this was a feature and intentionally broke the work-arounds in recent releases. I switched to abduco in most cases as a result.

                2. 2

                  This is what I use to decide whether to use tmux:

                  [ -z "$TMUX" ] && [ -z "$EMBEDDED" ] && { tmux attach || exec tmux new-session; }
                  

                  The $EMBEDDED flag is set for non-interactive shells, such as the one puma-dev uses when it cds into my Ruby app directory. I configure this in the LaunchAgent like so:

                     <dict>
                       <key>EMBEDDED</key>
                       <string>true</string>
                     </dict>
                  
                  1. 2

                    Nice!

                    I have a similar setup, but mine boots straight into my home tmux session, and then I have a hot key bound to open a session switching script in a pane.

                    The session switcher makes a list of active sessions and combines that with all the folders from my projects directory, which is then piped into fzf.

                    This is nice because I switch projects a lot, and I enjoy not having to care about the distinction of whether a session is running or not.

                    I also have a little bash function called clone that takes a URL to a git repo, makes a new session with that name and then clones it into my project folder.

                    With that setup I’ve found it’s generally faster to clone and run ag to search a repo than to use Github search.

                    1. 1

                      I am curious as to why not run tmux inside of vscode?

                      I am attempting a switch from tmux/emacs (session per project) to vscode (using one of the emacs bindings and project-mananger) but one of the things the project manager extension doesn’t do is maintain shell panels per project so I was thinking of running tmux (again session per project) and creating/auto attach to the session when the shell launches.