I am using tmux and zsh.
When I am outside tmux, zsh is customized with some Manjaro settings, as per the .zshrc
# Use powerline
USE_POWERLINE="true"
# Source manjaro-zsh-configuration
if [[ -e /usr/share/zsh/manjaro-zsh-config ]]; then
source /usr/share/zsh/manjaro-zsh-config
fi
# Use manjaro zsh prompt
if [[ -e /usr/share/zsh/manjaro-zsh-prompt ]]; then
source /usr/share/zsh/manjaro-zsh-prompt
fi
# Use ssh-agent
if [[ -e /home/marcosh/ssh-agent.zsh ]]; then
source /home/marcosh/ssh-agent.zsh
fi
source /usr/share/nvm/init-nvm.sh
When I am inside tmux though, the customizations are missing, eventhough it is using zsh. This is my .tmux.conf
set-option -g history-limit 50000
# sane scrolling
set-option -g mouse on
set -ga terminal-overrides ',xterm*:smcup#:rmcup#'
# new pane on same folder
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
# use zsh instead of bash
set -g default-command /usr/bin/zsh
What should I do to get the same zsh configuration both when inside and outside tmux?
The problem is tmux is not rendering the UTF-8 characters from the manjaro config properly.
To solve this use:
tmux -u
From the tmux manpage:
-u Write UTF-8 output to the terminal even if the first
environment variable of LC_ALL, LC_CTYPE, or LANG that
is set does not contain "UTF-8" or "UTF8". This is
equivalent to -T UTF-8.
I ssh into machines with shared config files but different tmux versions so I try to have a single version aware .tmux.conf. The following is for my tmux 2.6 machine.
My issue seems to be the nested ifs, or perhaps the nested quotes.
the following code hides the status bar when only one window is present:
if -F '#{==:#{session_windows},1}' 'set -g status off' 'set -g status on'; \
set-hook -g window-linked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"'; \
set-hook -g window-unlinked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"';
however my version guard, which adds double quotes around single quoted strings, breaks this
if-shell -b '[ "$(echo "$TMUX_VERSION >= 2.1" | bc)" = 1 ]' " \
if -F '#{==:#{session_windows},1}' 'set -g status off' 'set -g status on'; \
set-hook -g window-linked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"'; \
set-hook -g window-unlinked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"'; \
"
tmux starts up without any errors, but the status bar is not hidden.
You need to escape " inside other " as \".
Or put your inner commands in a separate config file and load it with source-file.
If you were using a later tmux version you could use {} to avoid this problem but on older tmux versions you will need to escape the quotes.
I would like to have tmux to automatically rename the window with the current working directory (cwd). As it is by default, it names the tab/window as the name of the current process, such as zsh or vim.
When I open a new window in tmux, the name is reattach-to-use-namespace and then it immediately switches to zsh.
I'm on OS X 10.10.2, I use zshell, and I have tmux 1.9a.
To be clear, I don't want the entire path in the name of the window, just the current directory, so for example, I want projectName, not /Users/username/Development/projectName.
If you want to see my current tmux.conf, here it is.
With tmux 2.3+, the b: format modifier shows the "basename" (or "tail") of a path.
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'
The FORMATS section of man tmux describes other modifiers, such as #{d:} and even #{s/foo/bar/:}.
With tmux 2.2 or older, the basename shell command can be used instead.
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#(basename "#{pane_current_path}")'
Expanding on what Josef wrote, you can put the basename of the directory in the status using a shell snippet:
# be sure to see note* below
set -g window-status-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
set -g window-status-current-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
# status bar updates every 15s by default**, change to 1s here
# (this step is optional - a lower latency might have negative battery/cpu usage impacts)
set -g status-interval 1
*Note that what would be ${pwd##*/} is escaped to ${pwd####*/} since # has special meaning in the format string.
**See here for an example default tmux config.
Show the top N components
Showing just the basename generates too much ambiguity, but full paths are too much clutter, so I settled for:
the/last/path
instead of:
/a/very/long/the/last/path
or just:
path
.tmux.conf
set-window-option -g window-status-current-format '#[fg=white,bold]** #{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]**|'
set-window-option -g window-status-format '#[fg=white,bold]#{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]|'
Trick taken from: Remove part of path on Unix
If that still does not solve ambiguity, I go for:
bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'
Tested on Tmux 2.1, Ubuntu 16.04.
To get the best of both worlds - window name is path when you're at a shell prompt, but name of executable when you're running something, try this:
set-option -g status-interval 1
set-option -g automatic-rename on
set-option -g automatic-rename-format "#{?#{==:#{pane_current_command},bash},#{b:pane_current_path},#{pane_current_command}}"
Replace "bash" with whatever shell you're using.
Adding this config to your ~/.tmux.conf file should work:
set-option -g window-status-current-format '#I:#{pane_current_path}#F'
set-option -g window-status-format '#I:#{pane_current_path}#F'
set-option -g status-interval 1
It depends however on your Tmux version. I wasn't able to make it work on 1.9a3 (in Cygwin) - but with Tmux 1.8 on Ubuntu (in Vagrant) it worked fine.
I use the following in ~/.tmux.conf to achieve this (working on OSX, zsh, tmux-2.3):
set -g automatic-rename-format '#{pane_current_path}'
set -g status-interval 5
You can set status-interval to 1 to make it respond faster to changing directories.
According to the changelog (https://raw.githubusercontent.com/tmux/tmux/master/CHANGES) this should work in tmux 1.9 and up.
Using ssh into a CentOS machine with tmux 2.3 the window name doesn't change until I press return in the new panel, not sure why that is happening.
Do something like this in a tmux session for zsh shell:
setopt PROMPT_SUBST
export PS1=$'\ek$(basename $(pwd))\e\\> '
If someone uses bash shell:
export PS1="\033k\$(basename \$(pwd))\033\\> "
You can add these commands in the shell initialization file on the condition the $TERM env variable is set to the value "screen"
I am using zsh hook for that
Add following in ~/.zshrc
precmd () {
if [ -n "$TMUX" ]; then
tmux set-window-option -q window-status-format "#[fg=cyan bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
tmux set-window-option -q window-status-current-format "#[fg=cyan, bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
fi
}
This doesn't strictly answer your question--it doesn't automatically rename an existing tmux session to the current working directory.
Rather, when creating a new session, it names that session after the current working directory.
Here's what I did:
to
~/.aliases
add
alias tm='tmux new -s `basename $PWD`'
Open a new terminal window and type:
tm
This now creates a new tmux session which is named after the current working directory.
Note: This relies on basename which does not exist in Windows.
I am sure that you want use this:
set -g status-left '#{pane_current_path} '
To change what you see in the window list you can specify a format when you define the key-binding for the chose-window function like this:
bind-key '"' choose-window -F "#{session_name} | #{window_name} - #{b:pane_current_path} (#{pane_current_command})"
I want to change the prefix if a environment variable is declared. I've tried the follwing configurations on .tmux.conf
This always change (with $SSH_CLIENT declared or not):
if-shell "test -n $SSH_CLIENT" "set -g prefix C-a"
and also:
if-shell "[[ -n $SSH_CLIENT ]]" "set -g prefix C-a"
But this code never change:
if-shell "[ -n $SSH_CLIENT ]" "set -g prefix C-a"
Finally, it worked, with the help from tmux-users mailing list, but there is still a problem with 1.8
# if if-shell goes in the beggining it will break the following command. this is going to be fixed after 1.8
# make c-a work as c-b
if-shell 'test -z "$SSH_CLIENT"' 'set -g prefix C-a'
http://sourceforge.net/mailarchive/forum.php?thread_name=20140201002909.GG11126%40yelena.nicm.ath.cx&forum_name=tmux-users
https://github.com/brauliobo/gitconfig/blob/master/configs/.tmux.conf
I have three windows:
1:zsh 2:vim* 3:htop
When I delete the current window (#2), I have these windows left:
1:zsh 3:htop
How can I make it so that it automatically renumbers them as
1:zsh 2:htop
If I recall correctly, this is the default behavior of GNU Screen. I know I could always :swap-window, but I would like to know if this is possible automatically.
Let's do it more simply.
If you are using tmux below version 1.7, append next line to ~/.tmux.conf:
bind-key C-s run "for i in $(tmux lsw|awk -F: '{print $1}'); do tmux movew -s \$i; done"
You could sort all windows, by typing PREFIX-KEY, then Ctrl + s.
Else, if you are using tmux version 1.7 or above, as already everybody says, append next line to ~/.tmux.conf:
set-option -g renumber-windows on
Since tmux 1.7, you can type just one command to do so:
tmux movew -r
This has now been implemented in C and submitted to tmux CVS on OpenBSD. Will hit the sourceforge portable release soon.
https://github.com/ThomasAdam/tmux-obsd/commit/c42e9b038dcdd36944e76954258a484387bd988f
The bash script below (updated version of [1] to reflect changes in tmux API) reorders tmux sessions. I suggest adding this as a bash function which you can call from any shell.
# re-number tmux sessions
for session in $(tmux ls | awk -F: '{print $1}') ;do
inum=0
for window in $(tmux lsw -t 0 | awk -F: '/^[0-9*]/ {print $1}') ;do
if [ ${window} -gt ${inum} ] ;then
echo "${session}:${window} -> ${session}:${inum}"
tmux movew -d -s ${session}:${window} -t ${session}:${inum}
fi
inum=$((${inum}+1))
done
done
[1] http://brainscraps.wikidot.com/tmux-renum