I want to do do this in tmux save-buffer - | pbcopy or some similar clone of save-buffer - | xclip -i -selection clipboard so that I can pipe the contents of the tmux clipboard to the os-x clipboard. The problem is that tmux always gives me a syntax hint when I run the command from the tmux command line, or it says save-buffer - | pbcopy returned 1 when run via keymap and does nothing useful. I've looked at the linux xclip solution on explainshell.com but that didn't help. The tmux man-page and error messages are also kinda useless. Any ideas?
Version info and the like:
tmux: 1.9a
OS-X: 10.9.4
ZSH: 5.0.5
I found an alternate solution to accomplish the same result... https://unix.stackexchange.com/questions/15715/getting-tmux-to-copy-a-buffer-to-the-clipboard
use copy-pipe on linux and os-x and I get what I'm looking for. :)
Sorry for necroposting.
As a nasty solution I suggest this:
copy text by tmux
run cat | pbcopy
paste from tmux
Ctr+D
Here you go. cat without args just gets input until EOF (which is accomplished by Ctd+D)
I solved this problem using reattach-to-user-namespace program and this configuration:
~/.tmux.conf
# Define default shell
set -g default-command "reattach-to-user-namespace -l zsh"
# Copy tmux buffer content to OS clipboard
bind-key y run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
Now I can press <prefix>+y to copy text from tmux buffer to OS clipboard.
I also wanted to be able to choose which buffer to copy from:
bind y choose-buffer "run-shell \"tmux show-buffer -b %% | pbcopy\" \; display-message \"Copied to system clipboard\""
Related
I have a process I want to monitor by tail -f on several output files in different directories. I can use a bash script start tmux as a detached session, create multiple panes, change to the top directory and reattach. This all works. My problem comes when I want the script to send more commands later. Is there some reason why once I attach, my script can't send commands or detach/reattach later? The reason to do more commands is that some files take 45 seconds to be created before I can tail them.
My example looks like
#!/bin/bash
# this depends on some settings from my ~/.tmux.conf
TopLevel='/tsload'
SimDir=`ls -d $TopLevel/SIM_ISS*`
# create and detach session
tmux new-session -s simwatch -n Sim_Watch -d
# make left & right panes, only 1 window
tmux split-window -h -t simwatch
# change to toplevel dir
tmux send-keys -t simwatch:1.1 "cd $SimDir" C-m
tmux send-keys -t simwatch:1.2 "cd $SimDir" C-m
tmux attach -t simwatch
At this point my script fails when I try to have it do more. I've also tried 'tmux detach -t simwatch' issue commands and reattach but they don't take effect.
Not very familiar with tmux, but from my experiment with your script, it looks like that tmux 'attach -t simwatch' is a blocking operation, i.e. it starts and will end once you detach/exit from the session, which is when the script will continue.
Regarding the issue with the files that appear later, if you know their paths, you can try to follow them with "tail -F", which will wait for the files to appear.
tail --follow=name --retry missing_file
I want to automatically run tmux after starting a rxvt terminal.
I used to do this by invoking tmux in my ~/.zshrc but this had the problem of having to hit ^D twice when I wanted to close my terminal.
I tried using rxvt -e tmux and rxvt -e zsh -c tmux to start tmux after starting rxvt which works.
The problem with this method is that I then don't get 256 color support in my terminal.
Any suggestions?
I think you'll need
tmux -2
See man tmux:
-2 Force tmux to assume the terminal supports 256 colours.
I have a script (.sh) and I want it to run in a existing tmux session. I have 1 session with 8 windows in.
Is there a command like tmux a -t session-name, which also specify the window?
And would a script like this work?
#!/bin/bash
tmux a -t session-name #What ever to write to specify window# java -jar -Xmx4G -Xms4G Spigot.jar
You can change the active window of a session before you attach to the session.
tmux -t <session-name> select-window -t <windowID>
tmux a -t <session-name>
You can combine two tmux commands as well.
tmux -t session-name select-window -t <windowID> \; a
If you really want to run java, presumably you want to create a new window with new-window, rather than select an existing one with select-window.
Newer versions of tmux (at least 1.9; did the above ever work, perhaps in 1.6?) no longer appear to have a -t option to specify the session to apply commands to. Instead, each individual command specifies the session.
tmux select-window -t <session-name>:<windowID> \; a -t <session-name>
For tmux version 2.1 this works
tmux a -t <session-name> \; select-window -t <windowID> \;
You can specify the window after the session separated by a colon.
tmux a -t session:window
You can even attach to a specific pane.
tmux a -t session:window.pane
Pane can be a number starting from 0. Window can be a number or name.
man tmux has more info about different syntaxes allowed for target-session, target-window, and target-pane.
target-window (or src-window or dst-window) specifies a window in the form session:window...
This syntax works on any other command like send-keys. If it's not working you may be on an older version of tmux and need to upgrade or try an approach suggested in the other answers.
I'm writing some small bash scripts for copiyng certain files/directories in GNU/Linux and Solaris. Everything is OK in Linux, but cp command hasn't the same options in Linux and Solaris.
Copy command is something like this:
cp -ruv $source $dest
Unfortunately I don't know how to achieve copy verbose and copy update in Solaris. Any idea?
Thanks
Unfortunately, cp under Solaris doesn't have that option. man solaris should reveal that.
Are you comfortable making your script depend on rsync?
Or, if possible, you can install the coreutils package and use GNU's cp.
I ran into a similar issue myself and found that gcp takes care of it too. I've made installing coreutils part of my standard system setup.
I run these on a new Solaris install:
pkgadd -d http://get.opencsw.org/now
pkgutil -U
pkgutil -i -y coreutils
pkgutil -a vim
pkgutil -i -y vim
pkgutil -i -y findutils
Remember to add the path - and the documentation path - to your profile, and possibly to the system profile at /etc/profile:
# Set the program path
PATH=$PATH:/usr/sfw/bin:/usr/sfw/sbin:/usr/openwin/bin:/opt/csw/bin:/usr/ccs/bin:/usr/local/bin:/usr/local
export PATH
# Set the documentation path
MANPATH="$MANPATH:/usr/share/man:/opt/sfw/man:/opt/csw/man"
export MANPATH
It sounds like you might be new to Solaris - as I am relatively new. I also do these, which shouldn't affect anything.
I set VIM as the default editor instead of VI - it's compatible, but has more features, including ANSI color, and some terminal emulators will pass your mouse clicks and scrolling through for even more flexibility:
# Set the default editor
EDITOR=vim
export EDITOR
Then if you are still using the default prompt that doesn't say anything, you might want to add some information - this version requires a Bash shell:
# Set the command prompt, which includes the username, host name, and the current path.
PS1='\u#\h:\w>'
export PS1
To recreate verbose mode, you can tee the output to the controlling terminal (/dev/tty) while the stdoout output of tee itself is passed to cp via xargs.
find /some/source/directory -type f | \
tee /dev/tty | xargs -I {} cp {} /copy/to/this-directory/
Replace the find with whatever you like, so long as it passes the paths to the files to be copied through the pipe to tee.
Tested on a standard Solaris 10 system without extra GNU utils.
I want to use a script to open a tmux session with 6 windows, each in a different directory. I started with a script I found and tried this first:
tmux new-session -s xyz -n etc -d 'cd /etc'
tmux new-window -t xyz:1 -n var 'cd /var/log'
But I soon found out that this will not work as I expected — the window will be closed after the shell command completes.
So my next idea was to start a new shell like this:
tmux new-session -s xyz -n etc -d 'cd /etc; bash -i'
tmux new-window -t xyz:1 -n var 'cd /var/log; bash -i'
tmux new-window -t xyz:2 -n var2 'cd /var/log; bash -i'
tmux new-window -t xyz:3 -n var3 'cd /var/log; bash -i'
tmux new-window -t xyz:4 -n var4 'cd /var/log; bash -i'
tmux new-window -t xyz:5 -n var5 'cd /var/log; bash -i'
tmux new-window -t xyz:6 -n var6 'cd /var/log; bash -i'
tmux select-window -t xyz:1
tmux -2 attach-session -t xyz
This almost works. But if I start more than about 4 windows, I frequently see the following bash errors in one of the windows after startup:
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
I have no idea why this happens, but I still think that I’m not doing this right. Is there a better way to set up a tmux session over several directories?
Tmuxinator is also really good for this. Basically you create setup files like so:
# ~/.tmuxinator/project_name.yml
# you can make as many tabs as you wish...
project_name: Tmuxinator
project_root: ~/code/rails_project
socket_name: foo # Not needed. Remove to use default socket
rvm: 1.9.2#rails_project
pre: sudo /etc/rc.d/mysqld start
tabs:
- editor:
layout: main-vertical
panes:
- vim
- #empty, will just run plain bash
- top
- shell: git pull
- database: rails db
- server: rails s
- logs: tail -f logs/development.log
- console: rails c
- capistrano:
- server: ssh me#myhost
Then you can start a new session with:
mux project_name
I've been using it for a while and have had a good experience for the most part.
The shell errors are probably due to some problem in your startup files (or something they run).
As shellter commented, temporarily including the command set -vx early in your startup sequence is a good way to find out where the errors are occurring.
If you find the -vx output too verbose, you could try “printf debugging” (manually adding debug statements to your startup files until you can narrow down exactly which lines are causing the errors):
Put echo start of .bashrc and echo end of .bashrc at the start/end of your .bashrc to see if the error occurs during your .bashrc. If not, instrument your other startup files: .bash_profile/.bash_login/.profile. If the errors happen before that file, then the problem may be in /etc/profile.
Once you know which file is being processed when the errors occur, add more debug outputs around each “major block” or line to narrow down the responsible section/line.
The errors may not actually be in your startup file itself, but in a script that it runs.
Note: These debug additions need to be temporary since they will cause problems if you ever use a program that makes automated logins (e.g. rsync, SSH-based Git access, etc.) since these programs expect a “clean” connection without such debugging noise present.
There should be no need to use cd command like that in the shell-command argument given to either tmux new-session or tmux new-window.
A new window will “inherit”† the current working directory when using new-session and new-window from the command line (i.e. when done through the tmux binary, instead of via a binding or at a tmux-: prompt). According to the CHANGES file, it looks like this has been the case since tmux 0.6 (at least for new-window).
† This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.
This script works for me with tmux 1.5:
#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz
# Start the session and window 0 in /etc
# This will also be the default cwd for new windows created
# via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d
# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
tmux new-window -t "$sn:$i" -n "var$i"
done
# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /
# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"
This might also (as a side-effect) alleviate your shell startup errors since the way tmux starts a shell is different from a plain bash -i (it is more akin to bash -l, which uses your .bash_profile/.bash_login/.profile instead of (just) your .bashrc).
You can use Teamocil instead. Teamocil is a simple tool used to automatically create sessions, windows and panes in tmux with YAML files.
For those who don't have ruby tmuxp is available in Python and supports both Teamocil and Tmuxinator style configuration files (in both yaml and json).
Source: https://github.com/tmux-python/tmuxp
Docs: https://tmuxp.git-pull.com
PyPI: https://pypi.python.org/pypi/tmuxp
Not to mention there is a very sweet library behind it all https://github.com/tmux-python/libtmux
pip:
pip install --user tmuxp
Above: Add ~/.local/bin/ to your $PATH for python user install programs
apt (Debian / Ubuntu / Mint):
sudo apt install tmuxp
Brew:
brew install tmuxp
Configuration
# ~/.tmuxp/workbench.yaml
session_name: workbench
windows:
- name: workbench
layout: main-vertical
panes:
- vim
- pane
- pane
tmuxp load workbench
You can load sessions directly: e.g. tmuxp load ./myworkspace.yaml
And finally, in your project-based config:
# tmuxp file aat project/.tmuxp.yaml
tmuxp load ./project
See documentation on tmuxp load for more information.
More configuration examples: https://tmuxp.git-pull.com/configuration/examples.html
I could not for the life of me get -c to work, so I worked around this by creating a script that is ran via send - this allows me to do whatever I want in each tmux session. In case it helps someone else, here it is:
#!/bin/bash
TMUX_SESSION=mystuff
TOP=~/mydir
tmux new-session -s "$TMUX_SESSION" -n $(pwd) -d
launch_my_window()
{
WINDOW=$1
NAME=$2
SUBDIR=$3
SCRIPT=$TMPDIR/tmux.sh.$WINDOW.$NAME
tmux new-window -t "$TMUX_SESSION:$WINDOW" -n "$NAME"
cat >$SCRIPT <<%%
cd $TOP/$SUBDIR
# do more stuff here
%%
chmod +x $SCRIPT
tmux send -t "$TMUX_SESSION:$WINDOW" $SCRIPT ENTER
sleep 1
}
launch_my_window 1 "stuff" subdir1
launch_my_window 2 "morestuff" subdir2
launch_my_window 3 "yetmorestuff" subdir3
#...
# Select window #1 and attach to the WINDOW
tmux select-window -t "$TMUX_SESSION:1"
tmux -2 attach-session -t "$TMUX_SESSION"
To simply open tmux with multiple panes and run some commands, I created the following bash file:
#!/bin/bash
tmux split-window -v -p 30 -c ~/path/to/folder1
tmux split-window -h -p 66 -c ~/path/to/folder2
tmux split-window -h -p 50 'mongod'
Run the bash file to get the following:
-----------------------------------
| |
|---------------------------------|
| folder1 | folder2 | mongod |
-----------------------------------
This works for me . Each pane opens in a different directory
alias tmx='tmux new-session -c ~/Pictures \; split-window -v -c /intranet/homenote/ \; split-window -h -c ~/Videos/ \;'