├── LICENSE.markdown ├── README.markdown ├── package.json └── tmux-up /LICENSE.markdown: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 James Ottaway 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # tmux-up 2 | 3 | Bootstrap new `tmux` sessions without complex tools, DSLs, or dependencies. 4 | 5 | ## Install 6 | 7 | If you use [fresh](http://freshshell.com/) to manage your dotfiles, just run: 8 | 9 | ``` 10 | fresh jamesottaway/tmux-up tmux-up --bin 11 | ``` 12 | 13 | If not, you might want to try [bpkg](http://www.bpkg.io/): 14 | 15 | ``` 16 | bpkg install jamesottaway/tmux-up 17 | ``` 18 | 19 | Alternatively just fetch the script with either `curl` or `wget`, and make sure it's executable: 20 | 21 | ``` 22 | curl -L https://git.io/tmux-up -o /usr/local/bin/tmux-up 23 | wget https://git.io/tmux-up -O /usr/local/bin/tmux-up 24 | chmod u+x /usr/local/bin/tmux-up 25 | ``` 26 | 27 | ## Usage 28 | 29 | Define the desired initial state of your `tmux` session in a file, using the standard `tmux` commands. 30 | 31 | For example, here is `dev.conf` for a fictional Rails application development environment: 32 | 33 | ``` 34 | send-keys 'git up' C-m 35 | send-keys 'git checkout develop' C-m 36 | send-keys 'bundle install' C-m 37 | new-window 38 | send-keys 'vim' C-m 39 | new-window -n server 40 | send-keys 'rails server' C-m 41 | new-window -n console 42 | send-keys 'rails console' C-m 43 | new-window -n db 44 | send-keys 'psql -d example_development' C-m 45 | ``` 46 | 47 | To create a new `tmux` session using the above configuration just run: 48 | 49 | ``` 50 | ~/example ❯ tmux-up dev.conf 51 | ``` 52 | 53 | Under the hood `tmux-up` will: 54 | 55 | - Create a `tmux` session named `example/dev` 56 | - Invoke each line in `dev.conf` 57 | - Switch to the first `tmux` window 58 | - Attach to the `example/dev` session 59 | 60 | If you detach from the `example/dev` session, simply re-run `tmux-up dev.conf` which will reattach you to the session. 61 | 62 | ## Alternatives 63 | 64 | There are quite a lot of other approaches to this problem already floating around. 65 | 66 | ### tmuxinator, teamocil, etc. 67 | 68 | These tools are very popular, but having a dependency on a working Ruby environment (or similar) for a simple tool like this seems like overkill. 69 | 70 | As a contrast, `tmux-up` is a simple shell script, meaning it will run anywhere `tmux` will. 71 | 72 | In addition to this, these tools commonly use abstracted formats to define your session configuration, which increases the difficulty of adopting such a tool. 73 | 74 | In the case of `tmux-up`, you use native `tmux` commands like `new-window` and `send-keys`. 75 | 76 | ### Plain ol' tmux 77 | 78 | The other end of the spectrum would be to invoke `tmux` directly, but override the configuration using the `-f` flag. 79 | 80 | I don't like this approach, for two reasons: 81 | 82 | - you need to add `source-file ~/.tmux.conf` (or wherever your default config lives) to ensure your top-level configuration is respected 83 | 84 | - you need to remember to append the `attach` command, since `tmux` always calls `new-session` when it starts 85 | 86 | To avoid these pitfalls, `tmux-up dev.conf` is functionally identical to `tmux -f dev.conf attach`. 87 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tmux-up", 3 | "version": "v0.5.1", 4 | "description": "Bootstrap new tmux sessions without complex tools, DSLs, or dependencies", 5 | "global": "true", 6 | "install": "install tmux-up ${PREFIX:-/usr/local}/bin", 7 | "scripts": [ "tmux-up" ] 8 | } 9 | -------------------------------------------------------------------------------- /tmux-up: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | TMUX_UP=tmux-up 4 | VERSION=0.5.1 5 | 6 | if [ $# -ne 1 ] && [ ! -f '.tmux.conf' ] 7 | then 8 | echo "$0: missing file operand" 9 | echo "Try '$0 --help' for more information." 10 | exit 1 11 | fi 12 | 13 | if [ "$1" = '-h' ] || [ "$1" = '--help' ] 14 | then 15 | cat <<-USAGE 16 | Usage: $0 [FILE] 17 | Attach to a newly bootstrapped or existing tmux session. 18 | 19 | The FILE argument is used to bootstrap the new tmux session if it doesn't 20 | already exist. It must contain tmux-compatible commands, which will be 21 | passed to tmux. Defaults to: .tmux.conf 22 | 23 | --help display this help and exit 24 | --version output version information and exit 25 | USAGE 26 | exit 0 27 | fi 28 | 29 | if [ "$1" = '-v' ] || [ "$1" = '--version' ] 30 | then 31 | cat <<-EOF 32 | $TMUX_UP $VERSION 33 | Copyright (C) 2015 James Ottaway 34 | License MIT: . 35 | This is free software: you are free to change and redistribute it. 36 | There is NO WARRANTY, to the extent permitted by law. 37 | EOF 38 | exit 0 39 | fi 40 | 41 | FILE=${1:-.tmux.conf} 42 | 43 | if [ ! -f "$FILE" ] 44 | then 45 | echo "$0: cannot stat ‘${FILE}’: No such file" 46 | exit 1 47 | fi 48 | 49 | BASE=$(basename "$PWD" | sed 's/[^a-zA-Z0-9\-]//g') 50 | NAME=$(basename "$FILE" .conf) 51 | 52 | if [ "$NAME" = 'tmux' ] || [ "$NAME" = '.tmux' ] 53 | then 54 | SESSION=$BASE 55 | else 56 | SESSION=$BASE/$NAME 57 | fi 58 | 59 | if ! tmux has-session -t "$SESSION" > /dev/null 2>&1 60 | then 61 | TMUX='' tmux new-session -d -s "$SESSION" 62 | xargs -L1 tmux < "$FILE" 63 | tmux select-window -t 1 64 | fi 65 | 66 | if [ -z "$TMUX" ] 67 | then 68 | tmux attach -t "$SESSION" 69 | else 70 | tmux switch-client -t "$SESSION" 71 | fi 72 | --------------------------------------------------------------------------------