Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Ubuntu Vim 從安裝到精通:基礎操作、ctags 程式碼導航、插件管理與 .vimrc 最佳實踐

編輯:Linux 系統常見問題解答

Vim 是 Linux 系統管理員、開發者的「終身編輯器」。本文從零開始:安裝、模式切換、核心編輯指令、ctags 程式碼跳轉、Vim-Plug 插件管理、生產級 .vimrc 配置,助你在終端裡像在 IDE 裡一樣高效編碼。

一、安裝與首啟

# Ubuntu / Debian
apt update && apt install -y vim vim-gtk3  # gtk3 支援系統剪貼簿

# 驗證
vim --version | head -1
# VIM - Vi IMproved 9.0 (2022 Jun 28, compiled ...)

# 首次啟動
vim
# 進入 Normal 模式,輸入 :help 閱讀內建教學

二、四大模式與核心指令

模式進入方式核心指令退出方式
Normal啟動預設 / Esch/j/k/l 移動、x 刪字、dd 刪行、yy 複製行、p 貼上、u 復原、Ctrl+r 重做、/pattern 搜尋、n/N 下/上一個輸入 : 進入 Command-line
Inserti 光標前、a 光標後、o 下新行、O 上新行正常輸入文字Esc 或 Ctrl+[
Visualv 字元、V 行、Ctrl+v 區塊選取後 d/y/c 刪/複製/改、>/< 縮進Esc
Command-line::w 存檔、:q 離開、:wq 存離、:e file 開檔、:set nu 顯示行號、:sp/:vs 分割視窗執行完畢自動回 Normal

三、ctags 程式碼導航(跨檔跳轉定義)

# 1. 安裝通用 ctags(支援 40+ 語言)
apt install -y universal-ctags

# 2. 專案根目錄生成 tags
cd /path/to/project
ctags -R --exclude=.git --exclude=node_modules --exclude=vendor .

# 3. Vim 內使用
# :set tags=./tags,tags;$HOME  # 向上遞歸查找 tags 檔
# Ctrl+]  跳轉到定義
# Ctrl+t  返回上一個位置
# :ts /tagname  列出同名標籤選單
# :tjump tagname  直接跳轉第一個

# 4. 自動更新(儲存時)
# 在 .vimrc 加入:
# autocmd BufWritePost * silent! !ctags -R --exclude=.git . &

四、Vim-Plug 插件管理(極速、並行、延遲載入)

# 1. 安裝 Plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

# 2. ~/.vimrc 關鍵片段
call plug#begin('~/.vim/plugged')

" 基礎增強
Plug 'tpope/vim-sensible'        " 合理預設值
Plug 'tpope/vim-surround'        " ys/cs/ds 成對符號操作
Plug 'tpope/vim-commentary'      " gcc/gcb 註解切換
Plug 'tpope/vim-repeat'          " . 重複插件動作

" 介面美化
Plug 'vim-airline/vim-airline'   " 狀態列
Plug 'vim-airline/vim-airline-themes'
Plug 'ryanoasis/vim-devicons'    " 圖示(需 Nerd Font)

" 程式碼導航
Plug 'preservim/tagbar'          " F8 顯示類/函數大綱
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'          " 模糊搜尋檔案/內容/歷史

" 語法/片段
Plug 'sheerun/vim-polyglot'      " 400+ 語法高亮
Plug 'SirVer/ultisnips'          " 代碼片段引擎
Plug 'honza/vim-snippets'        " 常用片段庫

" Git 整合
Plug 'tpope/vim-fugitive'        " :Git :Gdiff :Gblame
Plug 'airblade/vim-gitgutter'    " 行號旁顯示 +/-/~

call plug#end()

" 關鍵映射
nnoremap  :TagbarToggle
nnoremap  :Files
nnoremap  :Rg
inoremap    " 全能補全觸發
# 3. 安裝插件
vim +PlugInstall +qall

五、生產級 .vimrc 精簡版(可直接複製)

" ===== 基礎設定 =====
set nocompatible
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,big5
set number relativenumber
set cursorline
set scrolloff=5
set sidescrolloff=10
set wrap
set linebreak
set showbreak=↳
set tabstop=4 shiftwidth=4 expandtab smartindent
set autoindent
set backspace=indent,eol,start
set mouse=a
set clipboard=unnamedplus
set undofile
set undodir=~/.vim/undo
set history=1000
set wildmenu
set wildmode=longest:full,full
set ignorecase smartcase
set hlsearch incsearch
set splitbelow splitright
set completeopt=menu,menuone,noselect
set shortmess+=c
set updatetime=300
set timeoutlen=500
set ttimeoutlen=10

" ===== 外觀 =====
syntax enable
colorscheme desert
set termguicolors
highlight CursorLineNr cterm=bold gui=bold

" ===== 鍵盤映射 =====
let mapleader=" "
nnoremap w :w
nnoremap q :q
nnoremap h :nohlsearch
nnoremap  h
nnoremap  j
nnoremap  k
nnoremap  l
vnoremap <  >gv

" ===== 自動命令 =====
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType python setlocal ts=4 sts=4 sw=4 expandtab
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" ===== Plug 區塊(見上文) =====
" call plug#begin(...)
" ...
" call plug#end()

六、進階技巧:宏、暫存器、外部命令

技巧指令範例
錄製宏qa 開始錄製到暫存器 a,q 結束,@a 執行,10@a 執行 10 次批量在行尾加分號:qaA;jq 然後 100@a
暫存器"ayy 複製當前行到 a,"ap 貼上 a 內容,"+yy 複製到系統剪貼簿跨 Vim 實例複製
外部命令:!cmd 執行命令,:r !cmd 讀取輸出插入當前行,:range!cmd 範圍過濾:!python3 -m json.tool 格式化 JSON
多游標(視覺區塊)Ctrl+v 選取多行,I 插入,Esc 套用所有行批量加前綴

結語: Vim 學習曲線陡峭,但「配置一次、受用十年」。先內化 hjkl、Esc、:、i、dd、p、u、Ctrl+] 這八大核心,再漸進式加入插件、映射、宏。記住:**最好的 .vimrc 是你自己一行行寫出來的**。

Copyright © Windows教程網 All Rights Reserved