From 998c1c3fdaffcea72ed108550533ecc7e020f198 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 14 Jun 2023 10:56:08 +0200 Subject: [PATCH] tmux: Better simulate vim's C-e & C-y Keep the cursor position or selection while scrolling. --- .config/tmux/tmux.conf | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.config/tmux/tmux.conf b/.config/tmux/tmux.conf index 5cac9ab..02cc920 100644 --- a/.config/tmux/tmux.conf +++ b/.config/tmux/tmux.conf @@ -112,6 +112,28 @@ bind -T copy-mode-vi I send -X cancel bind -T copy-mode-vi a send -X cancel bind -T copy-mode-vi i send -X cancel +# Simulate vim's C-e & C-y (Keep cursor position while scrolling). As tmux +# cannot scroll past the first and last history line, both bindings stop +# scrolling when the respective line is visible. This mimics the behaviour of +# vim's C-y at the top of the file. +bind -T copy-mode-vi C-e \ + if -F "#{>:#{scroll_position},0}" { + send-keys -X scroll-down + if -F "#{>:#{copy_cursor_y},#{scroll_region_upper}}" { + send-keys -X cursor-up + } + } + +# TODO: C-y bugs shortly after the start for a couple of lines. I have no idea +# why, but I think the problem lays in the first if, as nothing happens at all +bind -T copy-mode-vi C-y \ + if -F "#{<:#{scroll_position},#{history_size}}" { + send-keys -X scroll-up + if -F "#{<:#{copy_cursor_y},#{scroll_region_lower}}" { + send-keys -X cursor-down + } + } + # Navigate panes with -[hjkl] # NOTE: C-[hjkl] (w/o prefix) moves through vim splits and tmux panes # See vim-tmux-navigator