remap pane navigation shortcuts for tmux - tmux

I want to remap the shortcuts to switch between panes. By default it's ctrl + b + arrow. I'd like to set it up more like vim: ctrl + b + hjkl. Is it possible to do it by changing ~/.tmux?

I am having this in my config, and it works as comment tells:
#switch panels
bind k selectp -U # switch to panel Up
bind j selectp -D # switch to panel Down
bind h selectp -L # switch to panel Left
bind l selectp -R # switch to panel Right

Related

How to make the middle mouse button paste from the primary selection in tmux when mouse mode is enabled?

In most terminal emulators Middle Mouse Button will paste from the X Windows primary selection. In tmux with mouse mode enabled (set -g mouse on) Middle Mouse Button no longer pastes from the primary selection because the mouse click is captured by tmux rather than by the terminal emulator. To paste you have to do Shift + Middle Mouse Button to pass the click through to the terminal emulator.
How can I make Middle Mouse Button paste without having to hold down Shift, even when mouse mode is enabled?
In tmux 3.2, this change was made.
CHANGES FROM 3.1c TO 3.2
Add a default binding for button 2 to paste.
Add this to your ~/.tmux.conf file:
# Make middle-mouse-click paste from the primary selection (without having to hold down Shift).
bind-key -n MouseDown2Pane run "tmux set-buffer -b primary_selection \"$(xsel -o)\"; tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"
Requires xsel to be installed. (sudo apt install xsel on Ubuntu.)
Explanation
Adds a Middle Mouse Button (MouseDown2Pane) binding that:
Uses tmux set-buffer to load the output of the xsel -o command (i.e. the contents of the primary selection) into a temporary tmux paste buffer named primary_selection
Uses tmux paste-buffer to paste the primary_selection buffer into the current pane
Uses tmux delete-buffer to delete the primary_selection buffer. This is to avoid messing with your buffers. tmux's paste command (Ctrl + b ] by default) pastes from the most-recently-created paste buffer. We don't want the primary_selection buffer to become your most-recently-created buffer and replace the previous buffer for the Ctrl + b ] binding. Nor do we want the primary_selection buffer to appear in your buffers list. So we delete it

Tmux: keep accidentally resizing pane instead of switching to the next one

Tmux is excellent, but there is one quirk that is proving to be a pane. If I hit ctrl+b then go too fast to the arrow key in order to switch panes I end up just resizing the current pane. It would be very nice to get rid of that behaviour. Is this a problem solvable in tmux or is in some kind of lag in my OS?
tmux is fully configurable so, yes, it's possible to solve this issue.
I suggest that you take a look at the tmux man pages or at other resources like Pragmatic Bookshelf's "tmux 2" book.
As an example, you can fully remap the keys to use to split, move around and resize windows adding something like this to your ~/.tmux.conf file:
# Splitting panes
bind | split-window -h # Uses "|" to split pane horizontally
bind - split-window -v # Uses "-" to split pane vertically
# Remapping movement keys
bind h select-pane -L # Move focus to pane on the left
bind j select-pane -D # Move focus to pane above the current one
bind k select-pane -U # Move focus to pane below the current one
bind l select-pane -R # Move focus to pane on the right
bind -r C-h select-window -t :- # Move to previous window
bind -r C-l select-window -t :+ # Move to next window
# Resizing panes (notes that is using the uppercase here and resize by 5 chars)
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

tmux jump 5 lines in history / copy mode

In my ~/.tmux.conf I have this which works great:
bind-key -t vi-copy n cursor-down
It allows me to move my cursor in history / copy mode.
I want to jump more rapidly (like I do in vim).
I want to jump 5 lines, but this doesn't work:
bind-key -t vi-copy E 5 cursor-up
How can I add a keybinding that will jump several lines?
Originally, tmux has only a restricted support for commands in copy-mode.
You can't assign multiple commands, you can't repeat them.
The only option is only assign a single command to a keystroke.
However, there's a mod for tmux that allows the full-fledged support for scripting in tmux: http://ershov.github.io/tmux/ (I'm the author)
Using this mod, you can do it this way:
bind-key -t vi-copy K tcl {
for {set i 0} {$i < 5} {incr i} {
cursor-up
}
}
Also, you'd be able to use variables, loops, define your own procedures and also have more control on tmux internals.

tmux: select pane by a letter after `prefix q`, instead of a number

I know I can select a timux pane directly by prefix q. I modified this default binding to bind / display-panes \; select-pane -t : in my config.
What I want is to have the selection not by the numbers that overlay each pane after C /, but by letters, say homerow HJKL or some sort of combination thereof.
In my config, I have this:
#switch panels
bind k selectp -U # switch to panel Up
bind j selectp -D # switch to panel Down
bind h selectp -L # switch to panel Left
bind l selectp -R # switch to panel Right
so that I can prefix + h/j/k/l to switch panel.

How do I make vim-style key bindings work on tmux using set-window-option?

I am aware that I can manually bind individual keys in my .tmux.conf file, but according to this page I should be able to get vim-like key bindings in tmux simply by adding the following to my .tmux.conf, saving me having to maintain a list of keybindings:
set-window-option -g mode-keys vi
But this doesn't really seem to work as expected. Sure enough, when I press Ctrl+b [ I am able to navigate through my terminal history using vim keys hjkl, but when I press Ctrl+b k it doesn't take me to the above window, for that I still have to press Ctrl+b Up, same goes for down, left and right.
Why doesn't this work? Do I really have to map these keys manually on top of vi mode to get actual vi mode?
I'm using tmux 1.6
add this to your cnofig:
#switch panels
bind k selectp -U # switch to panel Up
bind j selectp -D # switch to panel Down
bind h selectp -L # switch to panel Left
bind l selectp -R # switch to panel Right

Resources