From 35b203a71adf4e865373dc1e75a82095988bf900 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 10 Oct 2022 00:09:56 +0200 Subject: [PATCH] [tmux/vim] Use vim-tmux-navigator for seemless navigation between vim splits and tmux panes. --- .tmux.conf | 24 +++++++++++++++++++----- .vimrc | 8 +++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.tmux.conf b/.tmux.conf index 81e2be9..78de05f 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -11,11 +11,25 @@ set-option -g renumber-windows on # see https://github.com/neovim/neovim/wiki/FAQ#esc-in-tmux-or-gnu-screen-is-delayed set -sg escape-time 0 -# vim keys for switching between panes -bind-key -r k select-pane -U -bind-key -r j select-pane -D -bind-key -r h select-pane -L -bind-key -r l select-pane -R +# 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' +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 -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 'C-\' select-pane -l # vim mode keys in copy-mode set-window-option -g mode-keys vi diff --git a/.vimrc b/.vimrc index 38debd0..6381aa6 100644 --- a/.vimrc +++ b/.vimrc @@ -24,6 +24,9 @@ call plug#begin('~/.vim/plugged') " Send code to REPL Plug 'jpalardy/vim-slime' + " Seemless vim/tmux navigation + Plug 'christoomey/vim-tmux-navigator' + if has('nvim') " LSP helpers Plug 'neovim/nvim-lspconfig' @@ -79,11 +82,6 @@ call plug#end() :set tabstop=4 softtabstop=4 shiftwidth=4 expandtab smarttab autoindent "Nicer tab actions "Highlight without moving :nnoremap * *`` -"Move aroung splits without the C-W prefix -:nnoremap -:nnoremap -:nnoremap -:nnoremap "Easier fzf keybindings :nnoremap :Rg! :nnoremap (len(system('git rev-parse')) ? ':Files!' : ':GFiles!').""