提到「把 Vim 改造成現代編輯器」,社群裡十篇教學有九篇第一句話就是「先遷 Neovim + LazyVim」。但如果你已經在 Ubuntu 上用順手了 **經典 Vim 9 + vimplus + coc.nvim**,肌肉記憶全在逗號(,)鍵位上,其實完全沒必要推倒重來換 Lua 生態。

只需要改動 3 個使用者自訂檔案(絕不碰 ~/.vimplus/.vimrc),就能把經典 Vim 9 打磨成一套視覺與效能雙在線的現代暗色毛玻璃終端機編輯器——並且與前面幾篇打造的 Ubuntu 暗色霧面桌面、VS Code / Cursor 配色徹底融為一體:

  • 統一暗色毛玻璃視覺:Catppuccin Mocha 真彩色透出終端機霧面背景,同時讓 Pmenu 補全選單與 coc.nvim 懸浮文件保持深色實底 + 天藍圓角邊框,兼顧通透感與文字可讀性;
  • 現代互動細節:圓角膠囊風 vim-airline 狀態列/分頁列、極簡分割線、去除空行 ~ 波浪號、按模式自動切換終端機游標形態,以及用 Vim 9 原生 leadmultispace 取代傳統 indentLine 外掛;
  • 克制且扎實的效能最佳化:整套設定冷啟動實測約 130ms,限制超長行語法高亮與同步掃描深度,並實現真正 Buffer 級的 >2MB 大檔案自動降級,切回正常程式碼檔案零殘留。

先看本機實測成品效果(Ubuntu 22.04 LTS + Terminator 暗色毛玻璃 + Vim 9.2 + Sarasa Term SC Nerd):

Vim 9 + vimplus 暗色毛玻璃實機效果:Catppuccin Mocha 透底、NERDTree 圖示、Vim 9 原生細直線縮排提示與圓角膠囊狀態列


一、實測基線與零侵入三檔案邊界

整套方案既可以獨立重現,也可以作為本專欄 Ubuntu vimplus 安裝設定指南 的進階視覺與效能收尾篇。

維度 本機實測基線
系統與編輯器 Ubuntu 22.04 LTS + Vim 9.2 + vimplus + coc.nvim(clangd)
終端機與字型 Terminator(毛玻璃透底)+ Sarasa Term SC Nerd
配色體系 Catppuccin Mocha(與 VS Code / Cursor 保持同一色盤)
啟動速度 冷啟動實測約 131.8ms(含 NERDTree / Airline / LeaderF / Tagbar / coc.nvim)

為了保證後續升級或還原零心智負擔,所有改動嚴格限定在以下 3 個使用者層檔案,絕不修改 ~/.vimplus/.vimrc 本體:

  1. ~/.vimrc.custom.plugins:僅追加 1 行 catppuccin/vim 主題外掛;
  2. ~/.vimrc.custom.config:承載真彩色、分層透底、膠囊狀態列、Vim 9 原生縮排線與效能/大檔案鉤子;
  3. ~/.vim/coc-settings.json:為 coc.nvim 的自動補全、懸停文件與診斷浮窗啟用圓角邊框。

二、第一步:安裝 catppuccin/vim 主題外掛

在 ~/.vimrc.custom.plugins 中追加一行官方 Vim 主題外掛(附帶 catppuccin_mocha 配色及對應的 vim-airline 佈景主題):

1
2
" ~/.vimrc.custom.plugins
Plug 'catppuccin/vim', { 'as': 'catppuccin' }

在 Vim 中按 ,s 重載設定後執行 ,,i(或直接執行 :PlugInstall)安裝。如果終端機直連 GitHub 偶發逾時,也可以用單條淺複製指令一步寫入 vim-plug 目錄:

1
git clone --depth=1 https://github.com/catppuccin/vim.git ~/.vim/plugged/catppuccin

三、第二步:核心美化與效能最佳化設定(~/.vimrc.custom.config)

先給出可直接整段追加到 ~/.vimrc.custom.config 末尾的完整設定(每個關鍵取捨都順手寫明了原因,避開常見設定坑):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 現代終端機美化與效能最佳化(Catppuccin Mocha + 毛玻璃透底 + Buffer 層級大檔案保護)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" 1. 24-bit 真彩色(相容 tmux / screen 巢狀終端機,避免色彩退化為 256 色)
if has('termguicolors')
if !empty($TMUX) || &term =~# '^screen\|^tmux'
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
endif
set termguicolors
endif

" 2. 分層透明策略:主編輯區透出終端機毛玻璃,補全選單與 coc 浮動視窗維持深色實底
function! s:ApplyTransparentCatppuccin() abort
for l:grp in ['Normal', 'NonText', 'LineNr', 'SignColumn', 'EndOfBuffer', 'Folded', 'FoldColumn', 'StatusLineNC', 'TabLineFill']
execute 'highlight ' . l:grp . ' ctermbg=NONE guibg=NONE'
endfor
highlight VertSplit ctermbg=NONE guibg=NONE guifg=#45475a
highlight CursorLine ctermbg=NONE guibg=#2a2b3d
highlight CursorLineNr ctermbg=NONE guibg=NONE guifg=#89b4fa gui=bold
highlight LineNr guifg=#6c7086
highlight SpecialKey guifg=#45475a
highlight Whitespace guifg=#3b3d52
" 補全與 LSP 浮動視窗保留 Catppuccin Base 實底 + 天藍邊框,避免背景文字透出導致難讀
highlight Pmenu guibg=#1e1e2e guifg=#cdd6f4
highlight PmenuSel guibg=#3584e4 guifg=#ffffff gui=bold
highlight PmenuSbar guibg=#313244
highlight PmenuThumb guibg=#585b70
highlight CocFloating guibg=#1e1e2e guifg=#cdd6f4
highlight CocMenuSel guibg=#3584e4 guifg=#ffffff gui=bold
highlight FloatBorder guibg=#1e1e2e guifg=#89b4fa
endfunction

" 必須在執行 colorscheme 之前註冊 ColorScheme 自動命令,才能在首次載入主題時立刻觸發透底
augroup ModernVimBeautify
autocmd!
autocmd ColorScheme * call s:ApplyTransparentCatppuccin()
augroup END

" 安全切換 Catppuccin Mocha:新機尚未裝完外掛時優雅回退到 onedark,避免啟動報錯中斷
try
colorscheme catppuccin_mocha
let g:airline_theme = 'catppuccin_mocha'
catch /^Vim\%((\a\+)\)\=:E185/
colorscheme onedark
let g:airline_theme = 'onedark'
endtry

" 3. 極簡分割視窗直線 & 去除檔案末尾空行 ~ 波浪號
set fillchars+=eob:\ ,vert:│

" 4. 圓角膠囊風 Airline 狀態列(需 Sarasa Term SC Nerd 等 Nerd Font)
" 採用純 ASCII Unicode 跳脫寫法,網頁複製零亂碼,在 Vim 9 中與原字面量位元組層級等價
let g:airline_left_sep = "\ue0b4"
let g:airline_left_alt_sep = "\ue0b5"
let g:airline_right_sep = "\ue0b6"
let g:airline_right_alt_sep = "\ue0b7"
let g:airline#extensions#tabline#left_sep = "\ue0b4"
let g:airline#extensions#tabline#left_alt_sep = "\ue0b5"

" 不同模式自動切換終端機游標形狀(Normal 區塊 / Insert 細直線 / Replace 底線)
let &t_SI = "\e[6 q"
let &t_SR = "\e[4 q"
let &t_EI = "\e[2 q"

" 5. 互動手感與 Vim 9 原生細直線縮排提示(零外掛取代 indentLine)
set ttimeoutlen=10
set scrolloff=5
set sidescrolloff=5
set mouse=a
set number
set norelativenumber
set list
set listchars=tab:▸\ ,trail:·,leadmultispace:│\ \ \

" 如日後習慣相對行號跳轉(如 6j / 8k),取消下方註解即可
" set relativenumber

" 6. 渲染效能與 >2MB 大檔案 Buffer 層級防卡死保護
set synmaxcol=300
syntax sync maxlines=200
let g:NERDTreeExactMatchHighlightFullName = 0
let g:NERDTreePatternMatchHighlightFullName = 0

function! s:RestoreLargeFileEventIgnore() abort
if exists('s:large_file_saved_ei')
let &eventignore = s:large_file_saved_ei
unlet s:large_file_saved_ei
endif
endfunction

function! s:HandleLargeFilePre() abort
let l:file = expand('<afile>')
if empty(l:file)
return
endif
let l:size = getfsize(l:file)
if l:size > 2 * 1024 * 1024 || l:size == -2
let b:large_file = 1
let b:coc_enabled = 0
let b:current_syntax = 'largefile'
if !exists('s:large_file_saved_ei')
let s:large_file_saved_ei = &eventignore
endif
" 僅在讀入大檔案瞬間停用 FileType/Syntax,讀完立刻恢復,絕不汙染其他 Buffer
set eventignore+=FileType,Syntax
setlocal noswapfile undolevels=-1 bufhidden=unload
endif
endfunction

function! s:ApplyLargeFileLocal() abort
call s:RestoreLargeFileEventIgnore()
if get(b:, 'large_file', 0)
noautocmd setlocal filetype=largefile
setlocal syntax=OFF foldmethod=manual nowrap nolist nocursorline
let w:large_file_win_dirty = 1
elseif exists('w:large_file_win_dirty')
unlet w:large_file_win_dirty
setlocal foldmethod< wrap< list< cursorline<
endif
endfunction

augroup LargeFileOptimize
autocmd!
autocmd BufReadPre * call s:HandleLargeFilePre()
autocmd BufReadPost,BufWinEnter * call s:ApplyLargeFileLocal()
autocmd BufEnter * call s:RestoreLargeFileEventIgnore()
augroup END

這段設定裡藏著 5 處從源頭防踩坑的設計細節,值得順手點透:

  1. 為什麼 autocmd ColorScheme * 要寫在 colorscheme 前面?
    許多人在設定檔末尾先寫 colorscheme xxx 再裸寫一堆 highlight Normal guibg=NONE,一旦中途切換佈景主題或重載腳本,透明高亮立刻失效。提前註冊 ColorScheme 鉤子再執行 try/catch 切換主題,既能首次即生效,又能在新機還沒裝完 catppuccin 時安全備援退回 onedark。
  2. 為什麼必須採用「主背景透明 + 浮窗實底」分層設計?
    如果把 Pmenu 和 CocFloating 也做成透明,自動補全選單和懸浮文件蓋在程式碼上方時,底下透出來的彩色字元會與前景文字糊成一團。給浮窗單獨鎖定 Catppuccin Base 深色實底(#1e1e2e)、天藍邊框(#89b4fa)與高對比選取列(#3584e4),既有通透桌面氛圍,又保留 IDE 級的清晰可讀性。
  3. 為什麼 Airline 膠囊分隔符用 "\ue0b4"–"\ue0b7" 而不是直接貼字元?
    Powerline 擴充圓角字元屬於 Private Use Area(私用區),在網頁部落格裡直接貼字面值經常渲染成方塊 □,讀者複製後還會混入異常字元。在 Vim 9 裡雙引號跳脫 "\ue0b4" 與字面值字元位元組級完全等價,複製零翻車。
  4. 為什麼用 Vim 9 原生 leadmultispace 取代 indentLine,且設 ttimeoutlen=10?
    • indentLine 基於 conceal 機制重寫緩衝區渲染,既容易干擾 JSON/Markdown 引號顯示,在大檔案捲動時也會拖慢重繪;Vim 9 原生 listchars=...,leadmultispace:│\ \ \ 由 C 核心直接繪製,零外掛、零卡頓、不破壞文字;
    • vimplus 預設可能使用預設鍵碼逾時或 0,設成 ttimeoutlen=10 既能消除按 Esc 退出插入模式的肉眼延遲,又比 0 更穩妥(避免在 SSH / tmux 高抖動鏈路下誤拆方向鍵跳脫序列)。
  5. 為什麼大檔案鉤子不能簡單寫 syntax off,也不能只在 BufReadPre 設 filetype=largefile?
    • 網路上流傳最廣的大檔案一行流常寫 autocmd BufReadPre * ... | setlocal eventignore+=FileType foldmethod=manual noswapfile | syntax off——這裡其實有兩個全域暗坑:eventignore 是純全域選項,setlocal eventignore+=FileType 實際上會永久修改全域 &eventignore,導致同實例後續開啟的小檔案不再觸發 FileType;而 syntax off 會直接關掉整個 Vim 實例的語法高亮。
    • 此外,如果僅僅拿掉 eventignore 和 syntax off、只在 BufReadPre 裡寫 setlocal filetype=largefile,在 Vim 核心 readfile() 中 did_filetype 會在進入 BufRead 時被重設,filetypedetect 依然會觸發 *.cpp -> setf cpp,進而讓 tagbar 對幾 MB 的 .cpp 同步呼叫 ctags 導致卡死。
    • 上面的三段式鉤子註冊在 ~/.vimrc.custom.config 中,執行順序恰好位於 filetypedetect 之後、coc_nvim 與 TagbarAutoCmds 之前:BufReadPre 僅在讀入瞬間遮蔽 FileType,Syntax;BufReadPost / BufWinEnter(加 BufEnter 保底)第一時間恢復全域 &eventignore,同時對大檔案落地 noautocmd setlocal filetype=largefile + syntax=OFF 讓 coc.nvim 與 tagbar 0ms 跳過,並用視窗髒標記 w:large_file_win_dirty = 1 在切回普通小檔案時自動執行 setlocal foldmethod< wrap< list< cursorline< 完整還原視窗局部選項——真正做到大檔案秒開、小檔案零副作用。

四、第三步:為 coc.nvim 啟用圓角實底浮動視窗(~/.vim/coc-settings.json)

搭配上面配好的 CocFloating 與 FloatBorder 高亮群組,在 ~/.vim/coc-settings.json 中為懸停文件、補全彈窗與診斷浮窗統一開啟圓角邊框:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"suggest.noselect": true,
"coc.preferences.formatOnSave": false,
"hover.floatConfig": {
"border": true,
"rounded": true
},
"suggest.floatConfig": {
"border": true,
"rounded": true
},
"diagnostic.floatConfig": {
"border": true,
"rounded": true
}
}

存檔後重新開啟一個帶 compile_commands.json 的 C++ 檔案實測兩種高頻浮窗情境:

  1. 按 K 檢視符號懸浮文件:深色實底 #1e1e2e 浮窗配 #89b4fa 細圓角邊框浮在毛玻璃程式碼之上,函式簽章、參數型別與文件註解層次分明、絕不透底串行:

coc.nvim 懸浮文件圓角深色實底浮窗實機效果(#1e1e2e 實底 + #89b4fa 邊框)

  1. 進入 Insert 模式輸入 cache_. 觸發 clangd 補全:左下角狀態列無縫切換為薄荷綠 INSERT 膠囊、游標自動變為細直線(\e[6 q),同時彈出清晰的圓角深色實底補全候選框:

coc.nvim Insert 模式圓角補全彈窗與細直線游標實機效果


五、第四步:啟動耗時、設定狀態與大檔案隔離驗收

改完別靠感覺,用三條實測指令把速度、設定生效狀態與大檔案隔離全部跑一遍驗收:

1. 冷啟動耗時壓測

1
vim --startuptime /tmp/vim-startup.log +q && tail -n 15 /tmp/vim-startup.log

本機實測最後幾行如下(總計 131.8ms,日常敲 vim 幾乎無感秒開):

1
2
3
4
5
6
7
8
078.115  002.935  002.935: sourcing ~/.vim/plugged/vim-fugitive/autoload/fugitive.vim
079.517 000.961 000.961: sourcing ~/.vim/plugged/tagbar/autoload/tagbar.vim
094.370 000.292 000.292: sourcing ~/.vim/plugged/tagbar/autoload/tagbar/types/uctags.vim
130.744 080.976: opening buffers
130.884 000.043 000.043: sourcing ~/.vim/plugged/LeaderF/autoload/lfMru.vim
131.598 000.811: BufEnter autocommands
131.600 000.002: editing files in windows
131.792 000.031 000.031: sourcing ~/.vim/plugged/vim-airline/autoload/airline/extensions/tabline/buflist.vim

2. 主題與核心選項核對

在 Vim 指令列執行:

1
:echo g:colors_name &termguicolors &number &relativenumber

預期輸出如下(依序對應主題名稱、24-bit 真彩色開啟、絕對行號開啟、相對行號關閉):

1
catppuccin_mocha 1 1 0

3. >2MB 大檔案 Buffer 級隔離雙機實測

在 Ubuntu 22.04 與 Ubuntu 24.04 兩台實機上,分別建構 3MB+ 超大 .cpp 與普通小 .cpp,涵蓋 :edit large.cpp -> :edit small.cpp 以及指令列直開 vim large.cpp -> :edit small.cpp 兩種路徑,實測結果完全一致:

實測環境 測試樣本 大檔案載入狀態 同視窗切回小 .cpp 狀態
Ubuntu 22.04 + Vim 9.2 + coc.nvim + tagbar 3.2MB(38,000 行).cpp → 88B .cpp 0.0337s(33.7ms 秒開)
b:large_file=1、b:coc_enabled=0、ft=largefile、syn=OFF、synID=(空)、ul=-1、foldmethod=manual、list=0、cursorline=0
b:large_file=0、ft=cpp、syn=cpp;語法高亮群組完整生效(#include → cInclude、int → cType、字串 → cppString);list=1、cursorline=1 自動還原;&g:eventignore 為空且 g:syntax_on=1
Ubuntu 24.04 + Vim 9.1 + YouCompleteMe 3.3MB(100,000 行).cpp → 小 .cpp ft=largefile、syn=OFF、跳過重型解析秒開 ft=cpp、syn=cpp、cInclude/cType 高亮與 list=1 完整還原,零全域殘留

六、按需微調與一鍵備份還原

1. 三個高頻按需開關

需求情境 修改位置(~/.vimrc.custom.config)
想要純深色實底,不要毛玻璃透底 註解掉 augroup ModernVimBeautify 整塊(約 4 行),直接保留 catppuccin_mocha 原生 #1e1e2e 深色實底
習慣 6j / 8k 相對行號跳轉 取消註解 " set relativenumber 即可與 set number 組合成混合行號(目前行顯示絕對行號、上下顯示相對距離)
不想看到行尾多餘空白的小圓點 將 listchars 裡的 trail:·, 刪掉,改為 set listchars=tab:▸\ ,leadmultispace:│\ \ \

2. 一鍵備份與無損還原

動手前養成備份習慣,一行指令把涉及的 3 個使用者層檔案完整打個快照;隨時想退回原樣,一條指令無痕還原:

1
2
3
4
5
6
7
8
# 一鍵備份
mkdir -p ~/.local/share/vim-beautify-backup && \
cp -a ~/.vimrc.custom.plugins ~/.vimrc.custom.config ~/.vim/coc-settings.json ~/.local/share/vim-beautify-backup/

# 一鍵回滾(如需連外掛目錄一併清理,附加 rm -rf ~/.vim/plugged/catppuccin)
cp -a ~/.local/share/vim-beautify-backup/.vimrc.custom.plugins ~/.vimrc.custom.plugins && \
cp -a ~/.local/share/vim-beautify-backup/.vimrc.custom.config ~/.vimrc.custom.config && \
cp -a ~/.local/share/vim-beautify-backup/coc-settings.json ~/.vim/coc-settings.json

到這裡,經典 Vim 9 + vimplus 在視覺與效能上都徹底對齊了桌面暗色毛玻璃體系。如果你還希望把日常離不開的 VS Code / Cursor 細節精修、零外掛 tmux 膠囊狀態列(含 Vim 真彩色與 LSP 波浪線透傳)、git-delta / fzf / fd / zoxide 終端機效率鏈,以及 Typora / Guake / gedit 一併拉齊到同一套 Catppuccin Mocha 語言,可以繼續看下一篇收官實戰 👉 Ubuntu 常用開發軟體全鏈路美化與效率對齊:VS Code / Cursor / tmux / Git Delta / fzf / Typora 一站式實戰。


系列導覽

序號 文章標題 核心內容
#0 Ubuntu 開發工作站建置實戰 · 目錄 全鏈路架構藍圖、雙機基線與閱讀路線
#1 Ubuntu 22.04 GNOME 桌面環境設定指南 基礎起步:GNOME 擴充功能安裝、視窗習慣與 X11/Wayland 選型
#2 Ubuntu GNOME 暗色霧面玻璃桌面框架與雙檔案管理員透明度統一排錯(22.04 / 24.04) Fluent + Colloid + 動態桌布、Blur my Shell 調校與 Nautilus/Nemo 排錯
#3 Ubuntu 字型渲染、fontconfig 強綁定、fcitx5 美化與快捷鍵排錯實錄 Inter + HarmonyOS Sans SC + Sarasa、強綁定與輸入法排錯
#4 告別跨框架割裂:終端機 CLI 套件、Qt5、VS Code/Cursor 與 Chrome 暗色一體化 eza/bat/btop/fastfetch、qt5ct 融合配色、編輯器與 Chrome 無損腳本
#5 從電源鍵到鎖定畫面全鏈路閉環:GRUB、Plymouth、GDM 無損美化與一鍵還原 GRUB baseline JPEG、Plymouth bgrt、無損 GDM 與全域還原矩陣
#6 Ubuntu vimplus 安裝設定指南 零侵入改造 vimplus:coc.nvim + clangd、完整 LSP 鍵位與雙模式 Markdown 預覽
#7 **不遷 Neovim:零侵入將經典 Vim 9 + vimplus 改造成現代暗色毛玻璃編輯器**(目前篇) Catppuccin Mocha 透底、實底圓角浮窗、膠囊狀態列、Vim 9 原生縮排線與 Buffer 級大檔案最佳化
#8 Ubuntu 常用開發軟體全鏈路美化與效率對齊:VS Code / Cursor / tmux / Git Delta / fzf / Typora 一站式實戰 VS Code/Cursor 精修、零外掛 tmux 膠囊狀態列、delta/fzf/fd/zoxide 效率鏈與 Typora/Guake/gedit 收尾