1. 程式人生 > 實用技巧 >無外掛Vim配置檔案vimrc推薦與各VIM配置項解釋

無外掛Vim配置檔案vimrc推薦與各VIM配置項解釋

這裡有個女程式設計師寫的VIM配置檔案,寫的很好推薦下:

https://vimjc.com/vimrc.html

Vim 配置選項可以在 Vim 命令列模式下通過:set 配置選項xxx的形式執行,也可以通過set 配置選項xxx的格式保在配置檔案中被 Vim 載入執行。Vim的配置檔案 (例如~/.vimrc) 其實是多個個性化Vim 配置選項(Vim options)的集合。每個 Vim 配置項都有對應的關閉選項,即set no配置選項xxx可關閉配置選項xxx。例如,set number表示顯示行號,而set nonumber則表示不顯示行號。

Vim教程網介紹一個非常流行、且格式規範的無外掛型的配置檔案 (來自

https://github.com/wklken/vim-for-server,並通過分析每一個Vim配置選項的含義和功能。

:如果不太理解某個配置選項的含義,推薦使用vim -u NONE -N 某檔案xxx的方式使得 Vim 在啟動時不載入任何配置檔案,然後在命令列模式下單獨設定該配置項,通過檢視設定前後的變化來理解該配置項的含義和功能。(啟動引數-N會開啟nocompatible選項,防止進入Vi相容模式)。

1、Vim基礎配置

1
2
3
4
5
6
7
8
9
10
11
12
set nocompatible                " don't bother with vi compatibility "
set autoread " reload files when changed on disk, i.e. via `git checkout` "
set shortmess=atI

set magic " For regular expressions turn magic on "
set title " change the terminal's title "
set nobackup " do not keep a backup file "

set noerrorbells " don't beep "
set visualbell t_vb= " turn off error beep/flash "
set t_vb=
set timeoutlen=500

nocompatible用於關閉compatible,表示不與Vi相容。autoread表示如果當前檔案在 Vim 外被修改且未在 Vim 裡面重新載入的話,則自動重新讀取。

shortmess選項用於設定Vim縮短訊息長度的標誌位列表,例如,shortmess=m表示用 “[+]” 代替 “[Modified]”,推薦通過:h shortmess檢視shortmess選項的詳細介紹。

magic選項用於改變搜尋模式使用的特殊字元,推薦閱讀Vim搜尋字元轉義與magic搜尋模式title用於設定 Vim 視窗標題。

nobackup用於關閉backup,設定覆蓋檔案時不保留備份檔案。

noerrorbells用於關閉errorbells選項,表示 Vim 有錯誤資訊時不響鈴。visualbell表示使用可視響鈴代替鳴叫,而顯示可視響鈴的終端程式碼由t_vb選項給出。如
果既不想要響鈴也不想要閃爍,使用:set visualbell t_vb=實現。

timeoutlen表示以毫秒計的等待鍵碼或對映的鍵序列完成的時間,推薦閱讀Vim操作符待決模式(Vim Operator-Pending mode)。

2、Vim編碼設定

1
2
3
4
5
6
set encoding=utf-8  
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set fileformats=unix,dos,mac
set termencoding=utf-8
set formatoptions+=m
set formatoptions+=B

encoding設定 Vim 內部使用的字元編碼,它應用於緩衝區、暫存器、表示式所用的字元。fileencodings設定一個字元編碼的列表,表示 Vim 自動檢測檔案編碼時的備選字元編碼列表。fileformats用於設定參與自動檢測換行符 () 格式型別的備選列表。

termencoding用於設定終端使用的編碼方式。

formatoptions表示自動排版完成的方式。Vim 在視覺化模式下,可使用=進行程式碼格式的自動排版。m表示在任何值高於 255 的多位元組字元上分行;B表示在連線行時,不要在兩個多位元組字元之間插入空格。

3、Vim介面顯示設定

1
2
3
4
5
6
7
set ruler                       " show the current row and column "
set number " show line numbers "
set nowrap
set showcmd " display incomplete commands "
set showmode " display current modes "
set showmatch " jump to matches when entering parentheses "
set matchtime=2 " tenths of a second to show the matching parenthesis "

ruler用於顯示當前游標所在位置的行號和列號 (逗號分隔)。如果還有空間,在最右端顯示文字在檔案中的相對位置。

number用於設定顯示行號。nowrap設定超過視窗寬度的行不自動迴繞顯示。

showcmd用於設定在螢幕最後一行顯示 (部分的) 命令。showmode在插入、替換和可視模式裡,在最後一行提供訊息。

showmatch表示插入括號時短暫地跳轉到與之匹配的對應括號,而停留的時間由matchtime選項設定。如果置位 ‘showmatch’,matchtime表示顯示配對括號的十分之一秒。

4、Vim查詢配置

1
2
3
4
set hlsearch                    " highlight searches "
set incsearch " do incremental searching, search as you type "
set ignorecase " ignore case when searching "
set smartcase " no ignorecase if Uppercase char present "

hlsearch用於設定將搜尋結果高亮顯示,而incsearch選項會讓 Vim 根據已經在查詢域中輸入的文字,預覽第一處匹配目標;每當新輸入一個字元時,Vim 會即時更新預覽內容。

ignorecasesmartcase選項均開啟時,如果搜尋模式中包含大寫字母,Vim就會認為當前的查詢(搜尋)是區分大小寫的。如果搜尋模式中不包含任何大寫字母,Vim 則會認為搜尋應該不區分大小寫。這是個比較 ”智慧的” 推測你搜索意圖的機制。

推薦閱讀:Vim增量查詢與incsearch實時查詢預覽Vim搜尋命令使用方法和技巧

5、Vim Tab製表符設定

1
2
3
set expandtab                   " expand tabs to spaces "
set smarttab
set shiftround

expandtab選項用於設定在Vim插入模式下按下Tab鍵時,輸入到Vim中的都是空格。smarttab表示插入 Tab 時使用shiftwidth

shiftround表示縮排列數對齊到shiftwidth值的整數倍。參考:Vim自動縮排配置、原理和tab鍵製表符

6、Vim縮排配置

1
2
3
4
set autoindent smartindent shiftround                                                                                                                      
set shiftwidth=4
set tabstop=4
set softtabstop=4 " insert mode tab and backspace use 4 spaces "

autoindent用於設定新增加的行和前一行具有相同的縮排形式。smartindent選項用於設定新增行時進行”智慧”縮排,主要用於 C 語言一族,與cindent選項類似。在Vim smartindent 縮排模式下,每一行都有相同的縮排量,直到遇到右大括號 (}) 取消縮排形式。

shiftwidth選項用於設定執行Vim普通模式下的縮排操作 (<<>>命令 )時縮排的列數。而shiftround選項則表示縮排列數會自動取整到 ‘shiftwidth’ 選項值的倍數。

tabstop選項設定按下Tab鍵時,縮排的空格個數。

7、Vim顯示當前游標位置

1
2
set cursorcolumn
set cursorline

cursorcolumn設定高亮顯示游標當前所在列,cursorline設定高亮顯示游標所在螢幕行。更多內容,請閱:Vim快速跳轉任意行、任意列以及高亮顯示當前行、當前列

8、Vim檔案型別設定

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
filetype on
filetype plugin on
filetype indent on

autocmd FileType python set tabstop=4 shiftwidth=4 expandtab ai
autocmd FileType ruby set tabstop=2 shiftwidth=2 softtabstop=2 expandtab ai
autocmd BufRead,BufNew *.md,*.mkd,*.markdown set filetype=markdown.mkd

autocmd BufNewFile *.sh,*.py exec \":call AutoSetFileHead()\"
function! AutoSetFileHead()
" .sh "
if &filetype == 'sh'
call setline(1, "\#!/bin/bash")
endif

" python "
if &filetype == 'python'
call setline(1, "\#!/usr/bin/env python")
call append(1, "\# encoding: utf-8")
endif

normal G
normal o
normal o
endfunc

autocmd FileType c,cpp,java,go,php,javascript,puppet,python,rust,twig,xml,yml,perl autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()
fun! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfun

filetype on配置項是 Vim 檔案型別檢測功能的開關;filetype plugin on用於 Vim 開啟載入檔案型別外掛功能;filetype indent on用於指定 Vim 為不同型別的檔案定義不同的縮排格式。

autocmd FileType python set tabstop=4 shiftwidth=4 expandtab ai表示對於Python檔案 (通過autocmd命令指示 Vim 監聽FileType事件),自動設定 Tab 鍵對應的空格個數等。

autocmd BufNewFile *.sh,*.py exec \":call AutoSetFileHead()\"表示新建字尾為.sh.py的檔案時,自動執行 AutoSetFileHead 函式。AutoSetFileHead 函式基本的邏輯是在新檔案的首行自動插入部分內容,例如,新建 shell 指令碼自動新增#!/bin/bash”,然後新增兩個空白行 (通過normal Gnormal onormal o三行實現)。

:AutoSetFileHead 函式裡使用了normal命令,可以閱讀《Vim normal命令和重複操作》瞭解該命令的細節。

9、Vim按鍵對映配置

以下Vim按鍵對映配置的詳細功能介紹,請閱讀:《常用Vim命令及實用Vim按鍵對映配置詳解》。

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
nnoremap k gk   
nnoremap gk k
nnoremap j gj
nnoremap gj j

map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

nnoremap <F2> :set nu! nu?<CR>
nnoremap <F3> :set list! list?<CR>
nnoremap <F4> :set wrap! wrap?<CR>

set pastetoggle=<F5> " when in insert mode, press <F5> to go to "
" paste mode, where you can paste mass data "
" that won't be autoindented "
au InsertLeave * set nopaste
nnoremap <F6> :exec exists('syntax_on') ? 'syn off' : 'syn on'<CR>

inoremap kj <Esc>
nnoremap <leader>q :q<CR>
nnoremap <leader>w :w<CR>

map <Leader>sa ggVG"

" undo
nnoremap U <C-r>
nnoremap ' `
nnoremap ` '

nnoremap <silent> n nzz
nnoremap <silent> N Nzz
nnoremap <silent> * *zz
nnoremap <silent> # #zz
nnoremap <silent> g* g*zz
noremap <silent><leader>/ :nohls<CR>

vnoremap < <gv
vnoremap > >gv

map Y y$

nnoremap ; :

nnoremap H ^
nnoremap L $

cmap w!! w !sudo tee >/dev/null %

cnoremap <C-j> <t_kd>
cnoremap <C-k> <t_ku>
cnoremap <C-a> <Home>
cnoremap <C-e> <End>

nnoremap gk k表示將gk按鍵對映k,從Vim游標移動之實際行與螢幕行一文可知,gk表示上移一個螢幕行。

cnoremap <C-a> <Home>表示將<Ctrl> a組合鍵對映為Home鍵,實現在 Vim 命令列模式下 按<Ctrl> a移動游標到最前面,類似於《高效Linux技巧及Vim命令》一文提到的快速移動游標到行首的效果。