tmux : copy and paste without shift - tmux

i'm trying to follow a guide in which it's explained how to get copy/paste working without shift.
but with the following config :
set -g mouse on
set -g #plugin 'tmux-plugins/tpm'
set -g #plugin 'tmux-plugins/tmux-yank'
set -g #yank_action 'copy-pipe-no-clear'
set -g #override_copy_command 'xsel -i --clipboard'
set -g #yank_selection_mouse 'primary'
set -g #yank_selection 'primary'
bind -T copy-mode C-c send -X copy-pipe-no-clear "xsel -i --clipboard"
bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
I'm expecting to select something with the mouse, press y or Control-c, then go to any other app, press Control-v and have my selection pasted, but nothing happens.
when I list the keys I can see my bind:
bind-key -T copy-mode C-c send-keys -X copy-pipe-no-clear "xsel -i --clipboard"
and xsel works fine on its own :
$ echo hello | xsel -i --clipboard # C-V contains 'hello' as expected
What am i doing wrong?

Related

Iterm2 with tmux intergration not using keybindings from .tmux.conf

I have been using tmux in iTerm2 for a while now but I have not been using the tmux integration that now comes with iTerm2.
I started looking at using the tmux intergration as it allows you to use the shell intergration inside tmux.
The problem i am encountering is that after i do tmux -CC none of the key bindings inside my .tmux.conf work... I cant even get any of the defaults to work. I would really like to use tmux integration but cant unless i can get the key bindings in my .tmux.conf to work.
Some of the bindings that are not working are;
Rebound the prefix key to C-s
bind-key \ split-window -v -c '#{pane_current_path}'
bind-key - split-window -h -c '#{pane_current_path}'
These are just a couple of examples but basically nothing seems to be working...
My .tmux.conf is
# improve colors
set-option -g default-terminal "screen-256color"
# set base Prefix key to ctrl-s
unbind C-b
set -g prefix C-s
bind-key -r C-s send-prefix
# reload the source config
bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded"
# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
set-option -g status-keys "emacs"
#change colour of status bar
set-option -g status-bg '#666666'
set-option -g status-fg '#aaaaaa'
# set window split
bind-key - split-window -v -c '#{pane_current_path}'
bind-key \ split-window -h -c '#{pane_current_path}'
# set text in status bar
set -g status-interval 1
set -g status-justify centre # center align window list
set -g status-left-length 50
set -g status-right-length 140
set -g status-left '#[fg=green]#H #[fg=black]• #[fg=green,bright]#(uname -r | cut -c 1-6)#[default]'
set -g status-right '#[fg=green,bg=default,bright]#(tmux-mem-cpu-load -i 1) #[fg=red,dim,bg=default]#(battery -at) #[fg=white,bg=default]%a%l:%M:%S %p#[default] #[fg=blue]%Y-%m-%d'
# Fine adjustment (1 or 2 cursor cells per bump)
bind -n S-Left resize-pane -L 2
bind -n S-Right resize-pane -R 2
bind -n S-Down resize-pane -D 1
bind -n S-Up resize-pane -U 1
# new window opens in same directory
bind c new-window -c "#{pane_current_path}"
# set window numbering to start from 1
set -g base-index 1
set -g renumber-windows on
# allow to take a pane and put it into new window
bind-key b break-pane -d
# allows ctrl-j to open tree of tmux sessions
bind-key C-j choose-tree
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
# History
set -g history-limit 10000
# Mouse mode
set-option -g mouse on
# Rename Tab
set-option -g set-titles on
# Terminal notifier doesnt work with tmux
set -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL -l"
set-option -sg escape-time 10
Copying my answer at https://stackoverflow.com/a/47171067/4200039 over:
Someone reported a similar bug at iTerm2 Tmux Integration Default Keybindings not working in 2015. The response from the developer George Nachman suggests that keybindings are not supported:
One of the main goals of the tmux integration is that you use iTerm2's
keystrokes, not tmux's. If there's a specific workflow that you can't
accomplish natively, let me know--there is a lot of power in the
ability to remap keys in iTerm2.
This should be flagged as a duplicate of iTerm 2 not honoring key bindings declared in .tmux.conf but I can't flag it until it has an accepted or upvoted answer.

Cross compatible tmux configuration for vi mode

I have the following in my ~/.tmux.conf file
# improved (vi) copy paste
#
# vi mode in tmux
setw -g mode-keys vi
bind-key -t vi-copy y copy-selection
# select entire line
bind-key -t vi-copy v select line
Up till today this has always worked perfectly, problem is I've updated to the latest version :(.
tmux -V shows me that I'm currently at version 2.4
The vi-copy bindings are not working anymore. It seems that vi-copy mode is broken. If I execute the following:
CTRL+<leader> :list-keys -t vi-copy
The output is:
Unknown key-table vi-copy
According to this, it is a known issue
And you now have to do the following (extracted comment from the above link):
Basically you now need to bind your key in the copy-mode-vi table now,
look at how the default key bindings are done with "tmux lsk". For
your example: bind -Tcopy-mode-vi v send -X begin-selection
I'm sharing my tmux configuration across varous PC's, which are running different tmux versions, I hope somebody can explain how a cross compatible tmux configuration can be created, in which I can have the same keybindings and vi mode behaviour.
I have the following snippet in my tmux.conf:
if-shell "tmux -V | awk '{exit($2<2.4?0:1)}'" \
"bind-key -t vi-copy 'v' begin-selection; bind-key -t vi-copy 'y' copy-selection" \
"bind-key -T copy-mode-vi 'v' send -X begin-selection; bind-key -T copy-mode-vi 'y' send -X copy-selection"
Not the prettiest code I wrote but it seems to work.

How do I rename a pane in tmux?

How do I rename a pane in tmux?
Renaming a window
Ctrl-b ,
where Ctrl-b is the default prefix key.
Alternatively, run:
tmux rename-window <new name>
Or type Ctrl-b : rename-window <new name>.
Renaming a pane
In newer versions you can rename pane using:
tmux select-pane -T <title>
Or type Ctrl-b : select-pane -T <pane_name>.
Also, I have the following two lines in ~/.tmux.conf, in order to view the title of the pane in the top of the pane itself, and reformat the title.
set -g pane-border-status top
set -g pane-border-format " [ ###P #T ] "
yes you can rename pane names, and not only window names starting with tmux >= 2.3. Just type the following in your shell:
printf '\033]2;%s\033\\' 'title goes here'
you might need to add the following to your .tmux.conf to display pane names:
# Enable names for panes
set -g pane-border-status top
you can also automatically assign a name:
set -g pane-border-format "#P: #{pane_current_command}"
For those scripting tmux, there is a command called rename-window
so e.g.
tmux rename-window -t <window> <newname>
For those who want to easily rename their panes in a bash shell, this is what I have in my .tmux.conf
set -g default-command ' \
function renamePane () { \
read -p "Enter Pane Name: " pane_name; \
printf "\033]2;%s\033\\r:r" "${pane_name}"; \
}; \
export -f renamePane; \
bash -i'
set -g pane-border-status top
set -g pane-border-format "#{pane_index} #T #{pane_current_command}"
bind-key -T prefix R send-keys "renamePane" C-m
Panes are automatically named with their index, machine name and current command.
To change the machine name you can run <C-b>R which will prompt you to enter a new name.
*Pane renaming only works when you are in a shell.
Also when scripting, you can specify a name when creating the window with -n <window name>. For example:
# variable to store the session name
SESSION="my_session"
# set up session
tmux -2 new-session -d -s $SESSION
# create window; split into panes
tmux new-window -t $SESSION:0 -n 'My Window with a Name'
The easiest option for me was to rename the title of the terminal instead.
Please see: https://superuser.com/questions/362227/how-to-change-the-title-of-the-mintty-window
In this answer, they mention to modify the PS1 variable. Note: my situation was particular to cygwin.
TL;DR
Put this in your .bashrc file:
function settitle() {
export PS1="\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n$ "
echo -ne "\e]0;$1\a"
}
Put this in your .tmux.conf file, or similar formatting:
set -g pane-border-status bottom
set -g pane-border-format "#P #T #{pane_current_command}"
Then you can change the title of the pane by typing this in the console:
settitle titlename
You can adjust the pane title by setting the pane border in the tmux.conf for example like this:
###############
# pane border #
###############
set -g pane-border-status bottom
#colors for pane borders
setw -g pane-border-style fg=green,bg=black
setw -g pane-active-border-style fg=colour118,bg=black
setw -g automatic-rename off
setw -g pane-border-format ' #{pane_index} #{pane_title} : #{pane_current_path} '
# active pane normal, other shaded out​
setw -g window-style fg=colour28,bg=colour16
setw -g window-active-style fg=colour46,bg=colour16
Where pane_index, pane_title and pane_current_path are variables provided by tmux itself.
After reloading the config or starting a new tmux session, you can then set the title of the current pane like this:
tmux select-pane -T "fancy pane title";
#or
tmux select-pane -t paneIndexInteger -T "fancy pane title";
If all panes have some processes running, so you can't use the command line, you can also type the commands after pressing the prefix bind (C-b by default) and a colon (:) without having "tmux" in the front of the command:
select-pane -T "fancy pane title"
#or:
select-pane -t paneIndexInteger -T "fancy pane title"
To rename the pane, write the following in the tmux command prompt:
select-pane -T 'NEW PANE NAME'
To bind it to a key (example: u) write the following in your ~/.tmux.conf file:
bind u command-prompt "select-pane -T '%%'"
Then you can rename the pane just by <prefix>u
FYI:
To open the command prompt you need to <prefix>:
<prefix> is C-b by default (it can be changed with a set -g prefix NEW_KEY)
I use byobu with tmux as backend and would like to provide an alternative way: If you have Mouse-Mode on (for me Alt+F12), you can just right click onto the pane and select Rename. It appears that you can only rename the currently selected pane.

Tmux commands doesn't work

When I type ctrl+b(keep them pressing) button and then hit c button nothing happens. No ctrl+b command combinations work. Only these two commands work:
tmux new-session -s {session-name}
tmux kill-session -t {session-name}
Also I am not able to create new nested session. How to create new session. Are there modes for using tmux like vim. For eg. hit esc for normal/command mode, hit i for insert mode and v for visual mode. I am asking this question because I doubt if I need to press some key before giving key commands like ctrl+b+n. They just get written as normal text in terminal.
Characters are inputted in terminal. See the screenshot below. I am using all my tmux, zsh, vim configurations from here
Please check my tmux.config file
set -g default-command "reattach-to-user-namespace -l zsh"
# tmux display things in 256 colors
set -g default-terminal "screen-256color"
set -g status-utf8 on
# automatically renumber tmux windows
set -g renumber-windows on
# unbind default prefix and set it to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# for nested tmux sessions
bind-key a send-prefix
# Activity Monitoring
setw -g monitor-activity off
set -g visual-activity off
# Rather than constraining window size to the maximum size of any client
# connected to the *session*, constrain window size to the maximum size of any
# client connected to *that window*. Much more reasonable.
setw -g aggressive-resize on
# make delay shorter
set -sg escape-time 0
# make window/pane index start with 1
set -g base-index 1
setw -g pane-base-index 1
######################
#### Key Bindings ####
######################
# reload config file
bind r source-file ~/.tmux.conf \; display "Config Reloaded!"
# split window and fix path for tmux 1.9
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# synchronize all panes in a window
bind y setw synchronize-panes
# pane movement shortcuts
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+
# Resize pane shortcuts
bind -r H resize-pane -L 10
bind -r J resize-pane -D 10
bind -r K resize-pane -U 10
bind -r L resize-pane -R 10
# enable mouse support for switching panes/windows
# NOTE: This breaks selecting/copying text on OSX
# To select text as expected, hold Option to disable it (iTerm2)
setw -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on
# set vi mode for copy mode
setw -g mode-keys vi
# more settings to make copy-mode more vim-like
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Buffers to/from Mac clipboard, yay tmux book from pragprog
bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste); tmux paste-buffer"
You have changed the default escape-sequence in your configuration: from Ctrl-B (tmux default) to Ctrl-A (just like the similar terminal multiplexer screen).
The relevant configuration lines are in the third paragraph:
# unbind default prefix and set it to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
If you want to use tmux default one just comment out (with a leading #) or remove the lines above in your tmux.conf.
I have also recently came across the same problem in Linux Mint, but this thread fixed it
For the vertical splitting: Instead of (Ctrl+B) + % => (Ctrl+B) + (Ctrl+%).
For the horizontal splitting: Instead of (Ctrl+B) + " => (Ctrl+B) + (Ctrl+")
Also, do not forget to release (Ctrl+B) before pressing the (Ctrl+%) or (Ctrl+").

tmux -- disable echo of last run command?

I started using tmux recently and things are good except for the fact that tmux prints out my last run command before output, e.g.:
~ $ pwd
pwd/Users/me
You can see that it put "pwd" before the directory there, which is annoying.
My shell(zsh) doesn't do this when I run commands outside of tmux.
show-environment -g doesn't reveal any weird options being passed to zsh or anything: SHELL=/bin/zsh
I read through the manpage and Googled around but I can't find anything.
Thanks for any help!
Figured it out -- needed to change my ~/.tmux.conf to have a different TERM(xterm instead of screen-256color):
# act like vim
setw -g mode-keys vi
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind-key -r C-h select-window -t :-
bind-key -r C-l select-window -t :+
# act like GNU screen
unbind C-b
set -g prefix C-a
# look good
#set -g default-terminal "screen-256color"
set -g default-terminal "xterm"
set -g status "off"

Resources