{"id":66,"date":"2022-03-27T12:41:32","date_gmt":"2022-03-27T16:41:32","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=66"},"modified":"2025-03-05T14:00:43","modified_gmt":"2025-03-05T19:00:43","slug":"tmux-tips","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=66","title":{"rendered":"Tmux Tips"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"summary-of-commands\">Summary of commands<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tmux<\/code> create a new tmux session<\/li>\n\n\n\n<li><code>tmux ls<\/code> list any existing tmux sessions<\/li>\n\n\n\n<li><code>tmux a<\/code> reattach to the last open tmux session<\/li>\n\n\n\n<li><code>ctrl<\/code> + <code>b<\/code> the default tmux command prefix<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>d<\/code> detach from current tmux session<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>c<\/code> create a new window<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>0<\/code>&#8211;<code>9<\/code> to switch to the numbered window<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>,<\/code> rename the existing window<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>%<\/code> split the current pane into two vertical panes, left and right<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>\"<\/code> split the current pane into two horizontal panes, top and bottom<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>q<\/code> view numbered panes for current window<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>q<\/code>, <code>0<\/code>&#8211;<code>9<\/code> switch to pane immediately after displaying pane numbers<\/li>\n\n\n\n<li><code>prefix<\/code> + <code>z<\/code> to zoom and unzoom<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Creating Tmux sessions<\/h3>\n\n\n\n<p>To create a new Tmux session and attach to it, run the following command from the Terminal:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux<\/pre>\n\n\n\n<p>Or,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux new<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Detaching from Tmux sessions<\/h3>\n\n\n\n<p>To detach from a current Tmux session, just press <code>Ctrl+b<\/code> and <code>d<\/code>. You don&#8217;t need to press this both Keyboard shortcut at a time. First press <code>\"Ctrl+b\"<\/code> and then press <code>\"d\"<\/code>.<\/p>\n\n\n\n<p>Once you&#8217;re detached from a session, you will see an output something like below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[detached (from session 0)]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating named sessions<\/h3>\n\n\n\n<p>If you use multiple sessions, you might get confused which programs are running on which sessions. In such cases, you can just create named sessions.<\/p>\n\n\n\n<p>For example if you wanted to perform some activities related to web server in a session, just create the Tmux session with a custom name, for example <strong>&#8220;webserver&#8221;<\/strong> (or any name of your choice).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux new -s webserver<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">List Tmux sessions<\/h3>\n\n\n\n<p>To view the list of open Tmux sessions, run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux ls<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Attaching to Tmux sessions<\/h3>\n\n\n\n<p>You can attach to the last created session by running this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux attach<\/pre>\n\n\n\n<p>Or,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux a<\/pre>\n\n\n\n<p>If you want to attach to any specific named session, for example &#8220;myoffice&#8221;, run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux attach -t myoffice<\/pre>\n\n\n\n<p>Or, shortly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux a -t myoffice<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Kill Tmux sessions<\/h3>\n\n\n\n<p>When you&#8217;re done and no longer required a Tmux session, you can kill it at any time with command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux kill-session -t myoffice<\/pre>\n\n\n\n<p>To kill when attached, press <code>Ctrl+b<\/code> and <code>x<\/code>. Hit <code>\"y\"<\/code> to kill the session.<\/p>\n\n\n\n<p>You can verify if the session is closed with <code>tmux ls<\/code> command.<\/p>\n\n\n\n<p>To Kill Tmux server along with all Tmux sessions, run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux kill-server<\/pre>\n\n\n\n<p>Be careful! This will terminate all Tmux sessions even if there are any running jobs inside the sessions without any warning.<\/p>\n\n\n\n<p>When there were no running Tmux sessions, you will see the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux ls\n<strong>no server running on \/tmp\/tmux-1000\/default<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Split Tmux Session Windows<\/h3>\n\n\n\n<p>Tmux has an option to split a single Tmux session window into multiple smaller windows called <strong>Tmux panes<\/strong>.<\/p>\n\n\n\n<p>This way we can run different programs on each pane and interact with all of them simultaneously.<\/p>\n\n\n\n<p>Each pane can be resized, moved and closed without affecting the other panes. We can split a Tmux window either horizontally or vertically or both at once.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Split panes horizontally<\/h4>\n\n\n\n<p>To split a pane horizontally, press <code>Ctrl+b<\/code> and <code>\"<\/code><strong> <\/strong>(single quotation mark).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Split panes vertically<\/h4>\n\n\n\n<p>To split a pane vertically, press <code>Ctrl+b<\/code> and <code>%<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Split panes horizontally and vertically<\/h4>\n\n\n\n<p>We can also split a pane horizontally and vertically at the same time. Take a look at the following screenshot.<\/p>\n\n\n\n<p><a><\/a>First, I did a horizontal split by pressing <code>Ctrl+b<\/code><strong> <\/strong><code>\"<\/code> and then split the lower pane vertically by pressing <strong>Ctrl+b <\/strong><code>%<\/code>.<\/p>\n\n\n\n<p>As you see in the above screenshot, I am running three different programs on each pane.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Switch between panes<\/h4>\n\n\n\n<p>To switch between panes, press <strong>Ctrl+b<\/strong> and <strong>Arrow keys (Left, Right, Up, Down)<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Send commands to all panes<\/h4>\n\n\n\n<p>In the previous example, we run three different commands on each pane. However, it is also possible to run send the same commands to all panes at once.<\/p>\n\n\n\n<p>To do so, press <code>Ctrl+b<\/code> and type the following command and hit <code>ENTER<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:setw synchronize-panes<\/pre>\n\n\n\n<p>Now type any command on any pane. You will see that the same command is reflected on all panes.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Swap panes<\/h4>\n\n\n\n<p>To swap panes, press <code>Ctrl+b<\/code> and <code>o<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Show pane numbers<\/h4>\n\n\n\n<p>Press <code>Ctrl+b<\/code> and <code>q<\/code> to show pane numbers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Kill panes<\/h4>\n\n\n\n<p>To kill a pane, simply type <code>exit<\/code> and <code>ENTER<\/code> key. Alternatively, press <code>Ctrl+b<\/code> and <code>x<\/code>. You will see a confirmation message. Just press <code>\"y\"<\/code> to close the pane.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Zoom in and Zoom out Tmux panes<\/h3>\n\n\n\n<p>Now, I am going to zoom the left pane which has top command running. To do so, switch to left pane and press and <strong>z<\/strong>. The tmux prefix command is <strong>Ctrl+b<\/strong>, so I type <strong>Ctrl+b<\/strong> followed by <strong>z <\/strong>to zoom the pane. It is that simple!<\/p>\n\n\n\n<p><strong>Note:<\/strong> To switch between panes, press <strong>Ctrl+b<\/strong> and <strong>Arrow keys (Left, Right, Up, Down)<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Autostart Tmux Session On Remote System When Logging In Via SSH<\/h2>\n\n\n\n<p>To autostart Tmux session when connecting via SSH, edit your remote system&#8217;s <strong>~\/.bash_profile <\/strong>file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ nano ~\/.bash_profile<\/pre>\n\n\n\n<p>If the file is not available, just create it.<\/p>\n\n\n\n<p>And add the following lines in it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if [ -z \"$TMUX\" ]; then\n    tmux attach -t default || tmux new -s default\nfi<\/pre>\n\n\n\n<p>Save and close the file. Log out and log back into the remote systems. You will be landed into a new Tmux session named <strong>&#8216;default&#8217;<\/strong>.<\/p>\n\n\n\n<p>Now, start any process inside the Tmux session and detach it from by pressing <strong>Ctrl+b<\/strong> followed by <strong>d<\/strong>. The process will keep running inside the Tmux session even after the network connection is dropped. Once the network connect is back, SSH into the remote system and you will see that the job is still running!!<\/p>\n\n\n\n<p><a><\/a><a><\/a> Create session in tmux<\/p>\n\n\n\n<p><strong>Toggling Status Line<\/strong><\/p>\n\n\n\n<p>Turn it off:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux set-option status off<\/pre>\n\n\n\n<p>And, turn it on:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux set-option status on<\/pre>\n\n\n\n<p>You can also bind a key (t in this example) to toggle the status line on and off, as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ tmux bind-key t set-option status<\/pre>\n\n\n\n<p>Open a terminal and use the following command:<\/p>\n\n\n\n<p># <strong>tmux<\/strong><\/p>\n\n\n\n<p>There&#8217;s a difference between Session vs Window vs Pane (split) in tmux<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>Windows:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create windows using:<\/h3>\n\n\n\n<p>[ctrl + b] + c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Switching windows in a tmux session<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Previous<\/strong><\/td><td><strong>Next<\/strong><\/td><td><strong>n (0, 1, 2, 3 etc.)<\/strong><\/td><\/tr><tr><td>[ctrl + b] + p<\/td><td><a><\/a> [ctrl + b] + n<\/td><td>Switch to n Window: [ctrl + b] + 0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Switch using window list<\/strong><\/h3>\n\n\n\n<p>Another option uses [ctrl+b] + w<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Name your current window using:<\/strong><\/h3>\n\n\n\n<p>[ctrl+b] + ,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Name your tmux session using:<\/strong><\/h3>\n\n\n\n<p>tmux new -s sessionname<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">If you already created a session, rename it using:<\/h3>\n\n\n\n<p>[ctrl+b] + $<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Detach a session (come out of tmux session but keep it running in background)<\/h3>\n\n\n\n<p>use:<\/p>\n\n\n\n<p>[ctrl + b] + d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">List tmux sessions:<\/h3>\n\n\n\n<p>tmux ls<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reenter a session in Tmux (also known as attach)<\/h3>\n\n\n\n<p>tmux attach -t session_name_or_number<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating multiple panes in tmux<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Horizontal (Left\/Right)<\/td><td>Vertical (Up\/Down)<\/td><\/tr><tr><td><strong>[ctrl + b] + %<\/strong><\/td><td><strong>[ctrl + b] + \u201c<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Switching between panes<\/h3>\n\n\n\n<p>[ctrl+b] + arrow keys<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Zoom into or out of a pane<\/h3>\n\n\n\n<p>[ctrl+b] + z<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Killing or closing panes<\/h3>\n\n\n\n<p>[ctrl + b] + x<\/p>\n\n\n\n<p><strong>Resize panes<\/strong><\/p>\n\n\n\n<p>This assumes that you&#8217;ve hit ctrl + b and : to get to the command prompt<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>:resize-pane -L<\/li>\n<\/ul>\n\n\n\n<p>Here are some additional tmux pane resizing examples:<\/p>\n\n\n\n<ul style=\"font-style:normal;font-weight:400\" class=\"wp-block-list\">\n<li><code>:resize-pane -D (Resizes the current pane down)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -U (Resizes the current pane upward)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -L (Resizes the current pane left)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -R (Resizes the current pane right)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -D 10 (Resizes the current pane down by 10 cells)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -U 10 (Resizes the current pane upward by 10 cells)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -L 10 (Resizes the current pane left by 10 cells)<\/code> <\/li>\n\n\n\n<li><code>:resize-pane -R 10 (Resizes the current pane right by 10 cells)<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Killing sessions in tmux<\/h3>\n\n\n\n<p>tmux kill-session -t session_name_or_number<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Kill all tmux sessions<\/h3>\n\n\n\n<p>tmux kill-server<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Command<\/strong><\/td><td><strong>Explanation<\/strong><\/td><\/tr><tr><td>tmux kill-session -t name<\/td><td>Kill the mentioned session<\/td><\/tr><tr><td>tmux kill-session -a<\/td><td>Kill all sessions except the last used session<\/td><\/tr><tr><td>tmux kill-server<\/td><td>Kill all tmux sessions<\/td><\/tr><tr><td>tmux attach -t name<\/td><td>Attach\/enter the mentioned session<\/td><\/tr><tr><td>[Ctrl +b] + d<\/td><td>Detach\/exit the current session<\/td><\/tr><tr><td>[Ctrl +b] + $<\/td><td>Name or rename current session<\/td><\/tr><tr><td>[Ctrl +b] + c<\/td><td>Create new window<\/td><\/tr><tr><td>[Ctrl +b] + ,<\/td><td>Name or rename current window<\/td><\/tr><tr><td>[Ctrl +b] + w<\/td><td>List all windows<\/td><\/tr><tr><td>[Ctrl +b] + &amp;<\/td><td>Kill the current window<\/td><\/tr><tr><td>[Ctrl +b] + n\/p\/N<\/td><td>Move to next\/previous\/Nth window<\/td><\/tr><tr><td>[Ctrl +b] + %<\/td><td>Create horizontal pane<\/td><\/tr><tr><td>[Ctrl +b] + \u201c<\/td><td>Create vertical pane<\/td><\/tr><tr><td>[Ctrl +b] + space key<\/td><td>Toggle the layout of current pane<\/td><\/tr><tr><td>[Ctrl +b] + z<\/td><td>Zoom into the current pane<\/td><\/tr><tr><td>[Ctrl +b] + x<\/td><td>Kill the current pane<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Summary of commands Creating Tmux sessions To create a new Tmux session and attach to it, run the following command from the Terminal: $ tmux Or, $ tmux new Detaching from Tmux sessions To detach from a current Tmux session, just press Ctrl+b and d. You don&#8217;t need to press this both Keyboard shortcut at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,13],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-file-management","category-os-maintenance"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":8,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}