Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 啟用 Vim 語法高亮與環境優化完整設定

編輯:Linux 教學

核心概念:vi ≠ vim

CentOS 預設 vi 為精簡版,無語法高亮功能。需安裝 vim-enhanced 並設定別名將 vi 指向 vim。

四步驟完整設定

1. 安裝完整版 Vim(若未安裝)

yum install -y vim-enhanced

2. 設定 vi 別名指向 vim

echo "alias vi='vim'" >> /etc/profile
source /etc/profile

3. 啟用語法高亮

# 編輯系統級 vim 設定
vi /etc/vim/vimrc
# 確認或新增以下行
syntax on

4. 設定終端機色彩環境變數

echo "export TERM=xterm-color" >> /etc/profile
source /etc/profile

驗證與補充

  • 重新登入或執行 source /etc/profile 後,開啟任意程式碼檔案測試高亮效果。
  • 個人化設定可編輯 ~/.vimrc,常用範例:
set number          " 顯示行號
set tabstop=4       " Tab 寬度
set shiftwidth=4    " 縮排寬度
set expandtab       " Tab 轉空白
set autoindent      " 自動縮排
set hlsearch        " 搜尋高亮
set ignorecase      " 搜尋忽略大小寫

常見問題排查

  • 高亮不生效:確認 echo $TERM 輸出含 color 或 256color。
  • 遠端 SSH 無高亮:本地終端機需支援 256 色(如 Windows Terminal、iTerm2、Tabby)。
  • 系統級 vs 使用者級:/etc/vim/vimrc 影響所有使用者,~/.vimrc 僅影響當前使用者。
Copyright © Windows教程網 All Rights Reserved