{"id":1434,"date":"2014-03-18T18:01:28","date_gmt":"2014-03-18T18:01:28","guid":{"rendered":"http:\/\/www.nathankowald.com\/blog\/?p=1434"},"modified":"2016-11-13T16:41:12","modified_gmt":"2016-11-13T16:41:12","slug":"tmux-attach-session-alias","status":"publish","type":"post","link":"https:\/\/www.nathankowald.com\/blog\/2014\/03\/tmux-attach-session-alias\/","title":{"rendered":"tmux &#8211; &#8216;attach session&#8217; alias with tab completion"},"content":{"rendered":"<p>The command to reattach a tmux session is tedious to type.<br \/>\nIf you have many sessions open, or don&#8217;t remember the session name, you might type:<\/p>\n<pre class=\"prettyprint lang-sh\"># List sessions\r\ntmux ls\r\nlogs: 2 windows (created Thu Mar 13 13:07:19 2014) [237x62]\r\nturtle: 1 windows (created Tue Mar 18 13:57:21 2014) [237x65]\r\n\r\n# Attach to the session named turtle\r\ntmux attach -t turtle<\/pre>\n<h2>Create the alias<\/h2>\n<p>Lets create an alias to make attaching to tmux sessions easier.<br \/>\nThe alias will allow us to autocomplete tmux session names, avoiding <strong>tmux ls<\/strong>.<\/p>\n<h3>1. Bash completion<\/h3>\n<p>create a new file using your preferred editor:<\/p>\n<pre class=\"prettyprint lang-sh\">vim \/etc\/bash_completion.d\/tma<\/pre>\n<p>Paste the following.<\/p>\n<pre class=\"prettyprint lang-sh\">_tma() {\r\n    TMUX_SESSIONS=$(tmux ls -F '#S' | xargs)\r\n\r\n    local cur=${COMP_WORDS[COMP_CWORD]}\r\n    COMPREPLY=( $(compgen -W \"$TMUX_SESSIONS\" -- $cur) )\r\n}\r\ncomplete -F _tma tma<\/pre>\n<p>Save and quit.<\/p>\n<h3>2. Bash alias<\/h3>\n<p>Add the following to your <strong>~\/.bashrc<\/strong><\/p>\n<pre class=\"prettyprint lang-sh\">alias tma='tmux attach -t $1'\r\nif [ -f \/etc\/bash_completion.d\/tma ]; then\r\n. \/etc\/bash_completion.d\/tma\r\nfi<\/pre>\n<p>Source your<strong> ~\/.bashrc<\/strong> to pickup the bash_completion.<\/p>\n<pre class=\"prettyprint lang-sh\">source ~\/.bashrc<\/pre>\n<h3>3. Use tma<\/h3>\n<pre class=\"prettyprint lang-sh\">tma turt[Tab]<\/pre>\n<p>If a session named <strong>turtle<\/strong> exists, it will complete to: <strong>tma turtle<\/strong><br \/>\nPress enter and it will attach your tmux session.<\/p>\n<p>If you have one session open, pressing tab will autocomplete it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The command to reattach a tmux session is tedious to type. If you have many sessions open, or don&#8217;t remember the session name, you might type: # List sessions tmux ls logs: 2 windows (created Thu Mar 13 13:07:19 2014) [237&#215;62] turtle: 1 windows (created Tue Mar 18 13:57:21 2014) [237&#215;65] # Attach to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[22,7],"tags":[],"class_list":["post-1434","post","type-post","status-publish","format-standard","hentry","category-tmux","category-web-development"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/comments?post=1434"}],"version-history":[{"count":11,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions"}],"predecessor-version":[{"id":1763,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions\/1763"}],"wp:attachment":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/media?parent=1434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/categories?post=1434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/tags?post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}