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

Cannot get Control Arrow keys working in Vim through tmux

两周来我一直在努力解决这个问题,但没有成功。

我在 OSX 上使用 Iterm2,使用 tmux 作为会话管理器。我在 .vimrc

中设置了以下两个绑定的 VIM
"_____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>

现在奇怪的是 ctrl h 和 l 快捷键在向左箭头指示时工作得很好。

E349: No identifier under cursor

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

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

tmux.conf

#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

如果您取消注释这些行:

#set -g default-terminal "xterm-256color"
#set-window-option -g xterm-keys on

然后tmux

  • TERM 环境变量设置为定义这些键的终端描述,并且
  • 发送 vim 期望的 xterm 样式控制箭头键。

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

一个相关的问题是您无法从 OSX 终端使用 tmux 中的元箭头键调整窗格大小。解决方案是 select "Use Option as Meta key" 并删除 Keyboard 部分中与 option-arrow 对应的行。