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

Vim 進階配置大全:語法高亮、縮排、摺疊、多語言、按鍵映射一站式設定

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

本配置適用 Ubuntu 12.04+,涵蓋編程、中文支援、介面美化、高效編輯鍵盤映射,直接附加至 /etc/vim/vimrc 或 ~/.vimrc。

核心編輯增強

set nocompatible          " 關閉 vi 相容模式
syntax on                 " 語法高亮
colorscheme molokai       " 配色方案(需安裝)
set number                " 顯示行號
set cursorline            " 高亮當前行
set ruler                 " 狀態列標尺
set shiftwidth=4          " << >> 縮排寬度
set softtabstop=4         " 退格鍵刪 4 空格
set tabstop=4             " Tab 寬度
set expandtab             " Tab 轉空格
set autoindent            " 自動縮排
set smartindent           " 智慧縮排
set cindent               " C/C++ 專用縮排
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s

檔案與備份

set nobackup              " 不備份
set autochdir             " 自動切換工作目錄
filetype plugin indent on " 載入檔案類型插件
set backupcopy=yes        " 備份覆蓋模式

搜尋與介面

set ignorecase smartcase  " 搜尋忽略大小寫,含大寫則區分
set incsearch             " 輸入即時搜尋
set hlsearch              " 高亮搜尋結果
set noerrorbells          " 關閉錯誤響鈴
set novisualbell          " 關閉視覺響鈴
set t_vb=                 " 清空響鈴終端代碼
set showmatch             " 插入括號跳轉匹配
set matchtime=2           " 跳轉持續時間
set laststatus=2          " 永遠顯示狀態列
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)

摺疊功能

set foldenable
set foldmethod=syntax
set foldcolumn=0
setlocal foldlevel=1
" 空格鍵切換摺疊
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

多語言/中文支援

if has("multi_byte")
    set encoding=utf-8
    set termencoding=utf-8
    set formatoptions+=mM
    set fencs=utf-8,gbk
    if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
        set ambiwidth=double
    endif
endif

視窗/分頁/緩衝區快捷鍵

" 緩衝區切換
nnoremap <C-n> :bnext<CR>
nnoremap <C-p> :bprevious<CR>

" 分頁切換
nnoremap <C-t> :tabnext<CR>
nnoremap <C-T> :tabprevious<CR>

" 視窗分割切換(單鍵)
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" 檔案類型快速切換
nnoremap <Leader>1 :set filetype=xhtml<CR>
nnoremap <Leader>2 :set filetype=css<CR>
nnoremap <Leader>3 :set filetype=javascript<CR>
nnoremap <Leader>4 :set filetype=php<CR>

作業系統偵測與路徑變數

function! MySys()
    if has("win16") || has("win32") || has("win64") || has("win95")
        return "windows"
    elseif has("unix")
        return "linux"
    endif
endfunction

if MySys() == "windows"
    let $VIMFILES = $VIM . '/vimfiles'
else
    let $VIMFILES = $HOME . '/.vim'
endif

let helptags = $VIMFILES . '/doc'

這份配置讓 Vim 秒變現代化 IDE,適配中英文混合開發環境。

Copyright © Windows教程網 All Rights Reserved