Tmux - how to preserve window name after join-pane - tmux

When I do :join-pane -s :window-name in tmux, and than tmux ! to send window back it lost its name, is there a way to solve it?

Related

How do you switch from a tmux pane that you've scrolled up in to another tmux pane?

If I have two tmux panes open and I go to one and scroll back to the history using CTRL-B [, then for some reason I can't switch to the other pane without making the current pane scroll back to the bottom. Does anyone know how to work around this? Note that I use some custom tmux configuration, as described here, to make it easier to navigate between vim windows and tmux panes. Is this somehow messing up how tmux scrolling works?
Is it possible you're running into a conflicting default keybinding for tmux when it's in copy mode?
When tmux is running, you can check the currently active keybindings by doing prefix+?. When I do this on a bare tmux config, I can see that C-k is bound to send-keys -X copy-end-of-line, and it will move a buffer that's in copy mode to the end of the buffer, like you're describing. Alternatively, if you're using tmux's copy-mode-vi, then it looks like C-j is bound by default to send-keys -X copy-selection-and-cancel.
Try adding the following to your .tmux.conf:
unbind-key -Tcopy-mode C-k
unbind-key -Tcopy-mode-vi C-j
After this configuration update, I'm able to use C-j and C-k to navigate panes like described in the article you linked.

How to automate tmux setup from shell script?

I'd like to setup my tmux session from a shell script.
essentially I want to automate something like:
Create a session, split the window vertically, create a new window, run command 1 in the first vertical split, command 2 in the second, and another command in the second window.
Is that possible? So far I am trying to send keys but I'm not sure if there is a better way.
tmux new-session -s foo -d
tmux new-window -t foo
tmux send -t foo.0 "echo 1" ENTER
tmux send -t foo.1 "echo 2" ENTER
tmux attach -t foo
If you want to be able to later kill the program you are running and be left with a shell, then send-keys is the best bet.
If you don't, you can just start the program directly as part of new-session or new-window, for example:
tmux new-session -sfoo -d -- top
tmux new-window -tfoo -- emacs
tmux attach -tfoo
The remain-on-exit option is also useful for running programs like this.

Configure tmux copy mode to also dump a copy into the system clipboard

When working with multiple panes, it's convenient to use the tmux copy mode (ctrl+b [) to be able to use pane-aware copying. However, copy mode copies into a tmux copy buffer, rather than into the system clipboard. Ideally, I would like a copy to automatically copy into the system clipboard too.
A basic starting point was to add this alias to my bashrc:
alias tmux2clip='tmux save-buffer - | xsel -i -b' (on linux, requires xsel to be installed)
alias tmux2clip='tmux save-buffer - | pbcopy' (on macos)
After I copy using copy mode, I run the alias and the text gets copied into my clipboard! Works like a charm, but always requires that one extra manual step.
It would be ideal, if that alias can trigger as a kind of "post tmux copy hook" that I configure in tmux.
The best thing I could come up with is this line in my ~/.tmux.conf:
bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xsel -i -b"
# similarly, I believe only needed for keyboard based copies:
bind-key -T copy-mode C-w send-keys -X copy-pipe-and-cancel "xsel -i -b"
bind-key -T copy-mode M-w send-keys -X copy-pipe-and-cancel "xsel -i -b"
This causes any mouse based selection (note: I prefer mouse selection over keyboard based selections) to trigger a copy of that selection into my system clipboard. The annoying thing is though, that it will always happen, i.e. not only in copy mode, just anytime I select something it happens. This turns out to be quite sensitive in practice, just clicking the window with my mouse to give it focus often already accidentally triggers a small selection (often accidentally overwriting existing content in my clipboard, causing me to sometimes paste completely different stuff than intended).
I searched many similar questions but haven't been able to find any satisfying answers. I would really hope to find a way to get a kind of "post tmux copy hook" where I can insert my copy-to-clipboard command.
I use Ubuntu 18.04, with tmux 2.6. Please also be aware that I use mouse mode in my ~/.tmux.conf:
set -g mouse on
Any help would be highly appreciated. I've been wanting this for many months now already, and all of my revived efforts have only led to lost time :(
If you don't want tmux to enter copy mode when you drag on the pane, then unbind MouseDrag1Pane in the root table (unbind -n MouseDrag1Pane).

Is it possible to quit tmux, while "keeping" the window in the terminal

Use case
I run command over a bastion in SSH and since the connection can be dropped we are using tmux or screen.
For short commands I'm using send-keys, let's say I'm using a command like kubectl get pods. I'd like to keep the output of this command but terminate the tmux session.
Using remain-on-exit is not what I'm looking for as it keeps a dead window.
I've seen those question already
https://unix.stackexchange.com/questions/17116/prevent-pane-window-from-closing-when-command-completes-tmux
TMUX: how to make new window stay when start shell-command quits?
I'd like to keep the output of this command but terminate the tmux session.
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
I'm not sure it's exactly what you are looking for, but you could use the capture-pane command to save the contents of a pane (subject to what is still in the pane's history) to a file.

Different starting directory per window?

I daily use tmux (2.5) on my laptop to work, and my tmux sessions have a starting directory which is the working directory I started the tmux session from. Every pane/window I open start with this starting directory as working directory.
I can change this starting directory, and this change would apply to the whole session.
But if I want to work on a different project with several panes, I could start a new window, but every pane I would open in it would start with the session's starting directory : I would have to cd to the new location for each pane which isn't practical.
If I need to work on several project/directories simultaneously, I can start a new terminal session, then cd to the relevant directory/project and start a new tmux session. That's not complicated.
But if I want to do the same thing on a server through ssh, I'd need to either :
open a new ssh session.
either embed my remote tmux sessions in an other tmux session.
Neither sounds practical to me, I'd prefer a single tmux session on the remote machine.
I think it would be more convenient to being able to start new window with its own starting directory location that would apply to any new pane opened in it. Is there a way to achieve this?
Edit :
I already tried the -c parameter of tmux new-window command.
But it doesn't assign its starting directory to the window created this way, it only applies this custom starting directory to the first pane created.
Any new pane opened in this window then uses the session's starting directory as default working dir (and not the path passed to tmux new-window).
This question is very similar to: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux
It depends on your tmux version but the -c parameter does do the trick but it does not remember the setting. There used to be a default-path setting but that has been removed in version 1.9 unfortunately.
For newer versions you will need to pass along the -c in all cases (you can use an alias if you manually execute that command) or if you use key bindings you need to rebind the split/new window keys.
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
To use a custom path instead of the current pane path, execute this command:
tmux setenv custom_path /home/whatever/some/path
Put this in your config:
bind '"' split-window -c "#{custom_path}"
bind % split-window -h -c "#{custom_path}"
bind c new-window -c "#{custom_path}"
Yes, it turns out the -c option to the new-window command is what you are looking for: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux Also, this: https://unix.stackexchange.com/questions/101949/new-tmux-panes-go-to-the-same-directory-as-the-current-pane-new-tmux-windows-go
So either of tmux new-window -c $(pwd) or tmux new-window -c /path/to/dir inside your tmux session should do it.

Resources