|
" tmux aware navigation |
|
lua << EOF |
|
function pane_switch(direction) |
|
local vim_hotkeys = { up = "k", down = "j", left = "h", right = "l", } |
|
local tmux_hotkeys = { up = "U", down = "D", left = "L", right = "R", } |
|
local before_switch_winnr = vim.fn.winnr() |
|
vim.api.nvim_feedkeys( |
|
vim.api.nvim_replace_termcodes("<C-w>"..vim_hotkeys[direction], true, false, true), |
|
'nx', -- without `x` mode winnr will return same value for before/after |
|
false |
|
) |
|
local after_switch_winnr = vim.fn.winnr() |
|
-- run tmux command only when needed |
|
if (before_switch_winnr == after_switch_winnr) and (vim.fn.exists('$TMUX') == 1) then |
|
local obj = vim.system( |
|
{'tmux', 'select-pane', '-'..tmux_hotkeys[direction]}, |
|
{ text = true } |
|
):wait() -- Run synchronously |
|
end |
|
end |
|
|
|
-- create this mappings and function with autocommands |
|
-- :h lua-guide-autocommand-create |
|
-- :h lua-guide-autocommands-group |
|
vim.keymap.set('n', '<C-j>', function() pane_switch('down') end, |
|
{ desc = 'Tmux-aware switch pane to bottom' }) |
|
vim.keymap.set('n', '<C-k>', function() pane_switch('up') end, |
|
{ desc = 'Tmux-aware switch pane to up' }) |
|
vim.keymap.set('n', '<C-h>', function() pane_switch('left') end, |
|
{ desc = 'Tmux-aware switch pane to left' }) |
|
vim.keymap.set('n', '<C-l>', function() pane_switch('right') end, |
|
{ desc = 'Tmux-aware switch pane to right' }) |
|
EOF |
Created this gist, for several reasons (in order of their priority to me):