Ubuntu 預設安裝 vim-tiny (最小版),缺乏完整終端機相容性支援,導致 vi 編輯模式下方向鍵輸出 A B C D 亂碼而非游標移動。解決方法:移除 vim-common 並安裝完整版 vim。
vim-tiny 未編譯 +terminfo、+termcap 支援ESC [A = Up)~/.vimrc 設定# 1. 移除 minimal 版核心套件
sudo apt-get remove vim-common
# 2. 安裝完整版 vim (含 vim-runtime, vim-gui-common 等)
sudo apt-get update && sudo apt-get install vim
E: Package vim has no installation candidate
原因:本地套件索引過舊。解決:執行 sudo apt-get update 重新同步倉庫索引,再執行安裝。
vi --version
# 輸出應包含 +terminfo +termcap +mouse +clipboard 等特性
# 方向鍵在編輯模式正常移動游標
" ~/.vimrc
set nocompatible " 關閉 vi 相容模式
set backspace=indent,eol,start " 正常退格鍵行為
set number " 顯示行號
set cursorline " 高亮當前行
set hlsearch " 高亮搜尋結果
set incsearch " 增量搜尋
syntax on " 語法高亮
filetype plugin indent on " 檔案類型偵測與縮進
若不想更換套件,可嘗試在 ~/.bashrc 設定:
export TERM=xterm-256color
# 或在 vi 中輸入
:set term=builtin_ansi
但此法不穩定,強烈建議直接安裝完整版 vim。

核心動作:sudo apt-get remove vim-common && sudo apt-get update && sudo apt-get install vim。一鍵解決方向鍵亂碼,同時獲得完整 Vim 功能 (語法高亮、滑鼠支援、剪貼簿整合、外掛系統)。