christoomey / vim-tmux-navigator

Seamless navigation between tmux panes and vim splits
MIT License
5.07k stars 319 forks source link

How to disable pane-switching of Ctrl-{h,j,k,l} on tmux? #361

Closed rikkiemariko closed 9 months ago

rikkiemariko commented 10 months ago

I'd like to use Ctrl-{h,k} as linux terminal commands (Ctrl-h to delete a letter, Ctrl-k to delete-to-the-end-of-line) when I'm in a tmux pane.

So, I set the navigation command as Alt-{h,j,k,l} and they all work perfectly. BUT Ctrl-{h, j, k, l} switch the pane Tmux-to-Tmux and Tmux-to-Vim instead of working as linux commands inside the current pane. (Ctrl-{h,j,k,l} doesn't switch panes on Vim-to-Vim and Vim-to-Tmux as I expect.)

How can I disable pane-switching of Ctrl-{h, j, k, l} on tmux?

My .tmux.conf (I'm using mac, so Alt-{h,j,k,l} look weird.)

# enable mouse
set -g mouse on

# <prefix>r to refresh configuration
unbind r
bind r source-file ~/.tmux.conf

# set <prefix> as C-Space
unbind C-b
set -g prefix C-Space
bind C-Space send-prefix

# <prefix> \ to split pane vertically
# <prefix> - to split pane horizontally
# open panes in current directory
unbind %
unbind '"'
bind \\ split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# <prefix> m to maximize a pane
# <prefix> m to go back
bind -r m resize-pane -Z

# steart windows and panes at 1, not 3.0
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on

# set vi-mode
# set-window-option -g mode-keys vi

# copy and paste like vim
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# copy and paste with mouse intuitively
unbind -T copy-mode-vi MouseDragEnd1Pane

############## List of plugins ###############
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'egel/tmux-gruvbox'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux session after computer restart
set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves sessions for you every 15 minutes

# tmux gruvbox theme setup
set -g @tmux-gruvbox 'dark' # or 'light'
set -g status-position top

set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
set-option -sg escape-time 10
set-option -g focus-events on

# decide whether we're in a Vim process
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

bind-key -n '˙' if-shell "$is_vim" 'send-keys ˙' 'select-pane -L' # <A-h>
bind-key -n '∆' if-shell "$is_vim" 'send-keys ∆' 'select-pane -D' # <A-j>
bind-key -n '˚' if-shell "$is_vim" 'send-keys ˚' 'select-pane -U' # <A-k>
bind-key -n '¬' if-shell "$is_vim" 'send-keys ¬' 'select-pane -R' # <A-l>

tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'

bind-key -T copy-mode-vi '˙' select-pane -L # <A-h>
bind-key -T copy-mode-vi '∆' select-pane -D # <A-j>
bind-key -T copy-mode-vi '˚' select-pane -U # <A-k>
bind-key -T copy-mode-vi '¬' select-pane -R # <A-l>

# Initialize TMUX plugin manager (Keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

My nvim-tmux-navigation.lua

local status_ok, nvimTmuxNavigation = pcall(require, "nvim-tmux-navigation")
if not status_ok then
  return
end

nvimTmuxNavigation.setup{
  keybindings = {
                left = "˙", -- <A-h>
                down = "∆", -- <A-j>
                up = "˚", -- <A-k>
                right = "¬", -- <A-l>
  }
}

My keymaps.lua

local opts = { noremap = true, silent = true }

local term_opts = { silent = true }

-- Shorten function name
local keymap = vim.api.nvim_set_keymap

keymap("n", "˙", "<cmd>:wincmd h<cr>", opts) -- <A-h>
keymap("n", "∆", "<cmd>:wincmd j<cr>", opts) -- <A-j>
keymap("n", "˚", "<cmd>:wincmd k<cr>", opts) -- <A-k>
keymap("n", "¬", "<cmd>:wincmd l<cr>", opts) -- <A-l>

vim.cmd('let g:tmux_navigator_no_mappings = 1')
christoomey commented 10 months ago

It looks like you've got both the TPM tmux plugin installation, and the manual configuration (is_vim=... and the bind-key -n '˙' if-shell "$is_vim" 'send-keys ˙' 'select-pane -L' # <A-h>), as well as non-tmux-navigator config bind-key -T copy-mode-vi '˙' select-pane -L # <A-h>.

To make this work, I'd suggest the following:

rikkiemariko commented 9 months ago

How can I "remove the TPM installation in my tmux.conf"? I modified my .tmux.conf as below, and I ran "tmux source ~/.tmux.conf" but nothing has changed.

# enable mouse
set -g mouse on

# <prefix>r to refresh configuration
unbind r
bind r source-file ~/.tmux.conf

# set <prefix> as C-Space
unbind C-b
set -g prefix C-Space
bind C-Space send-prefix

# <prefix> \ to split pane vertically
# <prefix> - to split pane horizontally
# open panes in current directory
unbind %
unbind '"'
bind \\ split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# <prefix> m to maximize a pane
# <prefix> m to go back
bind -r m resize-pane -Z

# steart windows and panes at 1, not 3.0
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on

# set vi-mode
# set-window-option -g mode-keys vi

# copy and paste like vim
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# copy and paste with mouse intuitively
unbind -T copy-mode-vi MouseDragEnd1Pane

############## List of plugins ###############
# set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'egel/tmux-gruvbox'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux session after computer restart
set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves sessions for you every 15 minutes

# tmux gruvbox theme setup
set -g @tmux-gruvbox 'dark' # or 'light'
set -g status-position top

set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
set-option -sg escape-time 10
set-option -g focus-events on

# decide whether we're in a Vim process
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 '˙' if-shell "$is_vim" 'send-keys ˙' 'select-pane -L' # <A-h>
bind-key -n '∆' if-shell "$is_vim" 'send-keys ∆' 'select-pane -D' # <A-j>
bind-key -n '˚' if-shell "$is_vim" 'send-keys ˚' 'select-pane -U' # <A-k>
bind-key -n '¬' if-shell "$is_vim" 'send-keys ¬' 'select-pane -R' # <A-l>

tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'

# if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
#     "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
# if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
#     "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"
# bind-key -n 'C-Space' if-shell "$is_vim" 'send-keys C-Space' 'select-pane -t:.+'

# bind-key -T copy-mode-vi 'C-h' select-pane -L
# bind-key -T copy-mode-vi 'C-j' select-pane -D
# bind-key -T copy-mode-vi 'C-k' select-pane -U
# bind-key -T copy-mode-vi 'C-l' select-pane -R
# bind-key -T copy-mode-vi '˙' select-pane -L # <A-h>
# bind-key -T copy-mode-vi '∆' select-pane -D # <A-j>
# bind-key -T copy-mode-vi '˚' select-pane -U # <A-k>
# bind-key -T copy-mode-vi '¬' select-pane -R # <A-l>

# bind-key -T copy-mode-vi 'C-\' select-pane -l
# bind-key -T copy-mode-vi 'C-Space' select-pane -t:.+

# Initialize TMUX plugin manager (Keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
christoomey commented 9 months ago

I think you'll need to restart the tmux server (exit all tmux sessions and then restart) for the change to take effect as the server will still have the TPM code loaded.

I'm going to close this issue now as this seems to be more of a config issue than an actual problem with the plugin, but hopefully the notes above can get you back to working.