Refresh buffer while in Tmux copy mode? - tmux

While in copy mode (Ctrl+[) is there a way to refresh the buffer without having to leave copy mode?
This would be helpful while scrolling through long running output.

There's a refresh-from-pane (requires tmux 3.2+) command in copy mode. The default key binding for it is r.
To check if you tmux supports refresh-from-pane:
$ tmux list-keys -T copy-mode | grep refresh-from-pane
bind-key -T copy-mode r send-keys -X refresh-from-pane
$ tmux list-keys -T copy-mode-vi | grep refresh-from-pane
bind-key -T copy-mode-vi r send-keys -X refresh-from-pane

Related

Cannot copy text to clipboard on macOS with tmux from terminal session

The version I am running is tmux 3.3a
I cannot copy text to my clipboard when i scroll up in a terminal session. This seems to be because the text i'm highlightning is yellow in color rather than clear. I cannot scroll up in my tmux history and copy text to my clipboard with fn + cmd+c.
If I do not scroll the tmux session, copying to my clipboard works correctly when i press fn + cmd+c.
Here is an image that shows text that I cannot copy to my clipboard with fn + cmd+c.
Here is my tmux.conf
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# To copy, left click and drag to highlight text in yellow,
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
set -g visual-bell on
# https://stackoverflow.com/questions/32374907/tmux-mouse-copy-mode-jumps-to-bottom
unbind -T copy-mode-vi MouseDragEnd1Pane

How to write a tmux script so that it automatically split windows and opens a set of files?

I am new to Tmux. I know that you can write script to automate Tmux, in case your computer shuts down. I can write the following:
#!/bin/bash
tmux new-session -d -s MY_SESSION_NAME
tmux split-window -h
vim <path to file1>
This only opens up a 1 single vim editor page for file1, not in tmux, and not in any tmux split. Is it possible automate the file opening like this?
Here's an example of the automation I have on my tmux session.
Target a specific session/pane/window with <name>:<window>.<pane>, where window and panes are numbered, starting with 0.
Send-keys is the way to send a command to a particular tmux pane/window. the -d causes tmux to start in detached mode so you can keep sending more commands to it before actually attaching.
tmuxstart() {
tmux new-session -d -s sess >/dev/null
tmux rename-window -t sess:0 'main'
tmux splitw -v -p 10 -t sess:0.0
tmux splitw -h -p 80 -t sess:0.1
#required; otherwise pane numbering is bs
tmux select-pane -t sess:0.0
tmux splitw -h -p 5 -t sess:0.0
tmux send-keys -t sess:0.2 'sudo htop' Enter
tmux send-keys -t sess:0.1 'tmux clock -t sess:0.1' Enter
tmux select-pane -t sess:0.0
tmux new-window -t sess
tmux rename-window -t sess:1 'second'
tmux splitw -v -p 10 -t sess:1.0
tmux splitw -h -p 80 -t sess:1.1
tmux select-pane -t sess:1.0
tmux splitw -h -p 5 -t sess:1.0
tmux clock -t sess:1.1
tmux new-window -t sess
tmux rename-window -t sess:2 'scratch'
tmux splitw -v -p 10 -t sess:2.0
tmux select-pane -t sess:2.0
tmux splitw -h -p 5 -t sess:2.0
tmux clock -t sess:2.1
tmux select-window -t sess:0.0
tmux a -t sess
}

How can I create multiple tmux sessions containing windows from command line

I am trying to write a template script for a development session using tmux. So i just need to run this script for opening a new dev environment. Each session will have multiple windows - say two. First window(Window1) can be created while creating the detached session as:
tmux new-session -s $TMUX_SESSION_NAME -d -n Window1
(Here TMUX_SESSION_NAME is the argument passed to the script to name the session).
However, how can I create another window under the same session?
Note that I can create it as below but that messes up when creating another session. Although tmux ls shows each session having 2 windows each, the second session contains all the env settings of the first session(Both are for completely different projects)
tmux new-window -n Window2
tmux attach -t $TMUX_SESSION_NAME
I suspect both/all sessions go under the same /tmp/tmux-SOME_ID/default socket and hence this problem.
Note that the first time i start a dev session all is good with both windows.
Any ideas?
TL;DR: probably with something like
tmux new-window -t $TMUX_SESSION_NAME
tmux rename-window -t $TMUX_SESSION_NAME:1 'second'
More info (my configuration):
Here's what I use to start my tmux sessions. The argument to the function would be the name of the session you want to create.
If this does not answer your question, please comment and edit your question to that it is more clear to me.
tmuxstart() {
tmux new-session -d -s $1 >/dev/null
tmux rename-window -t $1:0 'main'
tmux splitw -v -p 10 -t $1:0.0
tmux splitw -h -p 80 -t $1:0.1
#required; otherwise pane numbering is bs
tmux select-pane -t $1:0.0
tmux splitw -h -p 5 -t $1:0.0
tmux send-keys -t $1:0.2 'sudo htop' Enter
tmux send-keys -t $1:0.1 'tmux clock -t $1:0.1' Enter
tmux select-pane -t $1:0.0
tmux new-window -t $1
tmux rename-window -t $1:1 'second'
tmux splitw -v -p 10 -t $1:1.0
tmux splitw -h -p 80 -t $1:1.1
tmux select-pane -t $1:1.0
tmux splitw -h -p 5 -t $1:1.0
tmux clock -t $1:1.1
tmux new-window -t $1
tmux rename-window -t $1:2 'scratch'
tmux splitw -v -p 10 -t $1:2.0
tmux select-pane -t $1:2.0
tmux splitw -h -p 5 -t $1:2.0
tmux clock -t $1:2.1
tmux select-window -t $1:0.0
tmux a -t $1
}

Tmux loading multiple enviroments from different files

Hello I would like to load multiple environments from different files. For example:
First File builds a session with 1 window and 3 panes.
Second File builds a session with 2 windows first window with 2 panes and second window with one pane.
Like:
tmux -f /path/to/file/basic.conf a
and then after detaching from the first session, i would like to load the same way the other enviroment.
tmux -f /path/to/file/scripting.conf a
but when i fire the second command i will attach to the first session (basic.conf).
But i would expect that when I fire the second script I would attach to the second session.
And tmux ls list only one session.
(The conf files by itself are running with no problem)
How is it possible to have multiple session build trough differnt conf files with tmux, and only with tmux no tmuxinator no tmuxp or anything else?
Or should i have one big conf file which builds everything that now is in muliply conf files?
basic.conf
SESSION_NAME="basic"
FIRST_WINDOW="shells"
SECOND_WINDOW="console"
source ~/.tmux.conf
new-session -s $SESSION_NAME -n $FIRST_WINDOW -d
split-window -h -t $SESSION_NAME
split-window -v -t $SESSION_NAME
new-window -n $SECOND_WINDOW -t $SESSION_NAME
select-window -t $SESSION_NAME:0.0
scripting.conf
SESSION_NAME="script"
FIRST_WINDOW="editor"
SECOND_WINDOW="console"
source ~/.tmux.conf
new-session -s $SESSION_NAME -n $FIRST_WINDOW -d
split-window -v -p 5 -t $SESSION_NAME
send-keys -t $SESSION_NAME:0.0 'cd ~/Code' C-m
send-keys -t $SESSION_NAME:0.0 'vim' C-m
send-keys -t $SESSION_NAME:0.1 'cd ~/Code' C-m
send-keys -t $SESSION_NAME:0.1 C-l C-m
new-window -n $SECOND_WINDOW -t $SESSION_NAME
send-keys -t $SESSION_NAME:1 'cd ~/Code' C-m
select-window -t $SESSION_NAME:0
I think I found a solution by myself.
The command:
tmux -f /path/to/file.conf a
should only be used when you would like to load another tmux config file instead of the default one.
If you like to start multiple sessions , which are preconfigured in files, then you have to do something like this:
tmux source-file -q .dotfiles/tmux/enviroments/basic.conf && tmux attach -t basic
Perhaps there is a better solution, but for now this solves my problem.
P.S. i made a little function to load the files less complicated.
function muxload(){
if [ -f ~/.dotfiles/tmux/enviroments/$1 ]; then
tmux source-file -q ~/.dotfiles/tmux/enviroments/$1 && tmux attach -t $1
fi
if [ -f ~/.dotfiles/tmux/enviroments/$1.conf ]; then
tmux source-file -q ~/.dotfiles/tmux/enviroments/$1.conf && tmux attach -t $1
fi
}
run it like this:
muxload {name_of_conf_file}

How to create a layout and run commands in at tmux launch?

I am trying to write a script I can run/source so tmux set a specific layout and run commands. I have some results.
Here is what I've written so far:
selectp -t 1
splitw -v -p 15
splitw -h -p 50
selectp -t 1
send-keys 'cd ~/code/octoly' Enter
send-keys 'vim .' Enter
selectp -t 2
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails c' Enter
new-window -d -n server -c ~/code/octoly
selectw -t 2
send-keys 'fd' Enter
splitw -h -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails s' Enter
selectp -t 1
splitw -v -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'be guard' Enter
What works more or less is the creation of the second window and the panes. Though the panes does not give me what I want. They are all created in the first window, none are created in the second.
Here is what I run to start tmux:
tmux new 'tmux move-window -t 99 \; source-file ~/.tmux/session_octoly'
Furthermore, where I'm really lost is that if I run each command by hand one by one, it gives me what I want.
What am I missing here?
First of all, you're using the -d flag in new-window that does not make the new window the current window:
If -d is given, the session does not make the new window the current window.
However I would probably spin up tmux in a different way, using tmux -f flag to use an ad-hoc config file that does what you want.
Please check the commands before running it in you're environment.
# File: ~/.tmux/octoly.conf
# Load default .tmux.conf
source-file ~/.tmux.conf
# setup octoly session
new-session -s octoly -n editor -d -c ~/code/octoly
send-keys 'vim .' Enter
split-window -v -p 15 -c ~/code/octoly
send-keys 'drails c' Enter
split-window -h -p 50 -c ~/code/octoly
# Select vim pane
select-pane -t 1
# create second window
new-window -n server -c ~/code/octoly
send-keys 'fd' Enter
split-window -h -p 50 -c ~/code/octoly
send-keys 'drails s' Enter
select-pane -t 1
split-window -v -p 50 -c ~/code/octoly
send-keys 'be guard' Enter
# Optional step, reselect window 1 (the one with vim)
select-window -t editor
Then you need to launch tmux using:
tmux -f ~/.tmux/octoly.conf attach
Another alternative would be to build a bash script sending the same commands.
Note: I've used the -c flag to specify the start directory instead of running every time a cd command. Given that every command is run in the same folder you can take them out in the split-window commands.

Resources