無外掛Vim配置檔案vimrc推薦與各VIM配置項解釋
這裡有個女程式設計師寫的VIM配置檔案,寫的很好推薦下:
Vim 配置選項可以在 Vim 命令列模式下通過:set 配置選項xxx
的形式執行,也可以通過set 配置選項xxx
的格式保在配置檔案中被 Vim 載入執行。Vim的配置檔案 (例如~/.vimrc
) 其實是多個個性化Vim 配置選項(Vim options)的集合。每個 Vim 配置項都有對應的關閉選項,即set no配置選項xxx
可關閉配置選項xxx。例如,set number
表示顯示行號,而set nonumber
則表示不顯示行號。
Vim教程網介紹一個非常流行、且格式規範的無外掛型的配置檔案 (來自
注:如果不太理解某個配置選項的含義,推薦使用vim -u NONE -N 某檔案xxx
的方式使得 Vim 在啟動時不載入任何配置檔案,然後在命令列模式下單獨設定該配置項,通過檢視設定前後的變化來理解該配置項的含義和功能。(啟動引數-N
會開啟nocompatible
選項,防止進入Vi相容模式)。
1、Vim基礎配置
1
|
set nocompatible " don't bother with vi compatibility "
|
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
|
set encoding=utf-8
|
encoding
設定 Vim 內部使用的字元編碼,它應用於緩衝區、暫存器、表示式所用的字元。fileencodings
設定一個字元編碼的列表,表示 Vim 自動檢測檔案編碼時的備選字元編碼列表。fileformats
用於設定參與自動檢測換行符 () 格式型別的備選列表。
termencoding
用於設定終端使用的編碼方式。
formatoptions
表示自動排版完成的方式。Vim 在視覺化模式下,可使用=
進行程式碼格式的自動排版。m
表示在任何值高於 255 的多位元組字元上分行;B
表示在連線行時,不要在兩個多位元組字元之間插入空格。
3、Vim介面顯示設定
1
|
set ruler " show the current row and column "
|
ruler
用於顯示當前游標所在位置的行號和列號 (逗號分隔)。如果還有空間,在最右端顯示文字在檔案中的相對位置。
number
用於設定顯示行號。nowrap
設定超過視窗寬度的行不自動迴繞顯示。
showcmd
用於設定在螢幕最後一行顯示 (部分的) 命令。showmode
在插入、替換和可視模式裡,在最後一行提供訊息。
showmatch
表示插入括號時短暫地跳轉到與之匹配的對應括號,而停留的時間由matchtime
選項設定。如果置位 ‘showmatch’,matchtime
表示顯示配對括號的十分之一秒。
4、Vim查詢配置
1
|
set hlsearch " highlight searches "
|
hlsearch
用於設定將搜尋結果高亮顯示,而incsearch
選項會讓 Vim 根據已經在查詢域中輸入的文字,預覽第一處匹配目標;每當新輸入一個字元時,Vim 會即時更新預覽內容。
當ignorecase
和smartcase
選項均開啟時,如果搜尋模式中包含大寫字母,Vim就會認為當前的查詢(搜尋)是區分大小寫的。如果搜尋模式中不包含任何大寫字母,Vim 則會認為搜尋應該不區分大小寫。這是個比較 ”智慧的” 推測你搜索意圖的機制。
推薦閱讀:Vim增量查詢與incsearch實時查詢預覽、Vim搜尋命令使用方法和技巧。
5、Vim Tab製表符設定
1
|
set expandtab " expand tabs to spaces "
|
expandtab
選項用於設定在Vim插入模式下按下Tab鍵時,輸入到Vim中的都是空格。smarttab
表示插入 Tab 時使用shiftwidth
。
shiftround
表示縮排列數對齊到shiftwidth
值的整數倍。參考:Vim自動縮排配置、原理和tab鍵製表符。
6、Vim縮排配置
1
|
set autoindent smartindent shiftround
|
autoindent
用於設定新增加的行和前一行具有相同的縮排形式。smartindent
選項用於設定新增行時進行”智慧”縮排,主要用於 C 語言一族,與cindent
選項類似。在Vim smartindent 縮排模式下,每一行都有相同的縮排量,直到遇到右大括號 (}) 取消縮排形式。
shiftwidth
選項用於設定執行Vim普通模式下的縮排操作 (<<
和>>
命令 )時縮排的列數。而shiftround
選項則表示縮排列數會自動取整到 ‘shiftwidth’ 選項值的倍數。
tabstop
選項設定按下Tab
鍵時,縮排的空格個數。
7、Vim顯示當前游標位置
1
|
set cursorcolumn
|
cursorcolumn
設定高亮顯示游標當前所在列,cursorline
設定高亮顯示游標所在螢幕行。更多內容,請閱:Vim快速跳轉任意行、任意列以及高亮顯示當前行、當前列。
8、Vim檔案型別設定
1
|
filetype on
|
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 G
、normal o
、normal o
三行實現)。
注:AutoSetFileHead 函式裡使用了normal
命令,可以閱讀《Vim normal命令和重複操作》瞭解該命令的細節。
9、Vim按鍵對映配置
以下Vim按鍵對映配置的詳細功能介紹,請閱讀:《常用Vim命令及實用Vim按鍵對映配置詳解》。
1
|
nnoremap k gk
|
nnoremap gk k
表示將gk
按鍵對映為k
,從Vim游標移動之實際行與螢幕行一文可知,gk
表示上移一個螢幕行。
cnoremap <C-a> <Home>
表示將<Ctrl> a
組合鍵對映為Home鍵,實現在 Vim 命令列模式下 按<Ctrl> a
移動游標到最前面,類似於《高效Linux技巧及Vim命令》一文提到的快速移動游標到行首的效果。