Vim (Vi Improved) 是 Linux 系統管理與開發必備編輯器。本文整理核心操作:模式切換、游標移動、編輯/刪除/復制/貼上、搜尋取代、縮排設定、行號顯示、Tab 寬度、儲存離開、檔案格式轉換 (dos2unix)。
| 按鍵 | 動作 |
|---|---|
| h/j/k/l | 左/下/上/右 |
| w/b/e | 下一字首/上一字首/字尾 |
| 0 / ^ / $ | 行首 / 第一非空白字元 / 行尾 |
| gg / G | 檔案首行 / 末行 |
| :N | 跳轉第 N 行 |
| Ctrl+f / Ctrl+b | 向下/上翻頁 |
/pattern 向下搜尋、?pattern 向上搜尋:%s/old/new/g 全檔取代 (加 c 逐一確認):s/old/new/g 當前行取代
:set cindent # C 風格自動縮排
:set shiftwidth=4 # 縮排寬度 4 空格
:set tabstop=4 # Tab 顯示寬度
:set expandtab # Tab 自動轉空格
:set autoindent # 新行跟隨上行縮排
# 臨時調整:選取視覺區塊 → < 或 > 左/右移
:set number # 顯示行號
:set nonumber # 隱藏行號
:set list # 顯示不可見字元 (Tab→^I, 行尾→$)
:set hlsearch # 高亮搜尋結果
:w 存檔、:wq / :x 存檔離開:q! 強制不存離開:w newfile 另存新檔
set nocompatible
set number
set tabstop=4 shiftwidth=4 expandtab
set autoindent cindent
set hlsearch incsearch
set encoding=utf-8
set fileencodings=utf-8,cp936,gb18030
syntax on
dos2unix filename # CRLF → LF
unix2dos filename # LF → CRLF
# 或在 vim 內
:set fileformat=unix
:w