Fixing the Home and End Key Issue in Vim Inside tmux
Problem Statement
While using Vim inside a tmux session in Alacritty, I noticed that the End key was scrolling the page instead of moving to the end of the line, and the Home key wasn't taking me to the beginning of the line as expected.
This issue did not occur when I used Vim in a normal terminal outside tmux, which indicated that tmux was somehow interfering with how Vim recognized key presses.
Why This Was Bothering Me Vim relies heavily on efficient navigation, and the Home and End keys are crucial when editing long lines of text. Instead of moving to the start or end of a line, the unexpected scrolling behavior made editing cumbersome. I had to manually move the cursor using 0 and $ commands in Vim, which slowed down my workflow.
Since this problem only happened inside tmux, I suspected that tmux was modifying the key sequences before Vim could process them.
Steps I Took to Fix the Issue Step 1: Checking What Keys Were Being Sent To confirm that tmux was altering the key sequences, I ran the following command inside tmux:
sh Copy Edit cat Then, I pressed the Home and End keys. This showed the escape sequences that were being sent.
Outside tmux, Home sent ^[[H, and End sent ^[[F (which Vim understood correctly). Inside tmux, the sequences were different (^[[1~ for Home and ^[[4~ for End), which caused the incorrect behavior. This meant tmux was remapping my keys incorrectly.
Step 2: Fixing Key Sequences in tmux Since tmux was interfering with the key sequences, I had to ensure it preserved the correct mappings.
I added the following lines to my ~/.tmux.conf file:
sh Copy Edit set -g terminal-overrides 'xterm*:smkx=\E[?1h\E=,rmkx=\E[?1l\E>' set -ga terminal-overrides ',xterm-256color:Rs=\E[m:Km=\E[M' Then, I reloaded the tmux configuration:
sh Copy Edit tmux source-file ~/.tmux.conf This forced tmux to respect proper escape sequences for function keys.
Step 3: Ensuring Vim Recognized the Keys Even with the tmux fix, Vim still needed proper mappings. I added these lines to my ~/.vimrc:
vim
Copy
Edit
" Fix Home and End keys in tmux
if &term =~ "tmux"
set term=xterm-256color
map
Step 4: Setting the Correct $TERM Value To prevent similar issues, I made sure tmux was using a proper terminal type.
I checked my terminal type using:
sh Copy Edit echo $TERM Inside tmux, it should be screen-256color or tmux-256color. Outside tmux, it should be xterm-256color. To enforce this setting, I added the following to my ~/.tmux.conf:
sh Copy Edit set -g default-terminal "screen-256color" set -as terminal-overrides ",screen-256color:Tc" Then, I restarted tmux:
sh Copy Edit tmux kill-server tmux Conclusion This fix resolved my issue, allowing the Home and End keys to function correctly inside tmux. The problem was caused by tmux modifying the escape sequences sent to Vim. By tweaking tmux’s terminal-overrides and adjusting Vim’s key mappings, I ensured that both tools handled key inputs properly.
Now, Vim behaves consistently whether I'm using it inside or outside tmux, making my workflow much smoother. 🚀