无法通过 tmux 使控制箭头键在 Vim 中工作

And*_*ich 6 macos vim terminal tmux

两周以来,我一直试图弄清楚这一点,但没有运气。

我在 OSX 上使用 Iterm2,使用 tmux 作为会话管理器。我在 VIM 中设置了以下两个绑定.vimrc

"_____Easy change tab left and right____
 nnoremap <C-Left> :tabprevious<CR>
 nnoremap <C-Right> :tabnext<CR>
 nnoremap <C-h> :tabprevious<CR>
 nnoremap <C-l> :tabnext<CR>
Run Code Online (Sandbox Code Playgroud)

现在奇怪的部分是 ctrl h 和 l 快捷方式工作完美,而左箭头表示。

E349: No identifier under cursor

这似乎与应该发生的事情无关(它在 TMUX 之外完美运行!),但是使用检查我的映射:map没有显示任何冲突的映射。但我就是想不通。任何帮助都将不胜感激。下面是我的 tux.conf 文件。由于这在 tmux 之外完美运行,我不相信 vimrc 应该与此有任何关系。

我有一种感觉,它的 Iterm2 正在向终端发送奇怪的东西,而 tmux 正在误解它们,但我不知道如何解决它。

配置文件

#set -g default-terminal "xterm-256color"
set -sg escape-time 0

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf

# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Enable mouse mode (tmux 2.1 and above)
set -g mouse on

# don't rename windows automatically
set-option -g allow-rename off

# Use Alt-arrow keys without prefix key to switch panes
bind -n M-S-Left previous-window
bind -n M-S-Right next-window

# Dont clear screen after other program is on
set-window-option -g alternate-screen on
#set-window-option -g xterm-keys on
Run Code Online (Sandbox Code Playgroud)

Tho*_*key 5

如果您取消注释这些行:

#set -g default-terminal "xterm-256color"
#set-window-option -g xterm-keys on
Run Code Online (Sandbox Code Playgroud)

然后tmux

  • 设置 TERM环境变量定义这些键的终端描述,以及
  • 发送 vim 期望的 xterm 风格的控制箭头键。

如果没有这两个更改,TERM则设置为screen(未定义键),并将tmux为控制箭头键发送不同的序列(碰巧不在任一终端描述中)。