無插件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 用於顯示當前光標所在位置的行號和列號 (逗號分隔)。如果還有空間,在最右端顯示文本在文件中的相對位置。

Vim-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 會即時更新預覽內容。

當 ignorecase 和 smartcase 選項均打開時,如果搜索模式中包含大寫字母,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快速跳轉任意行、任意列以及高亮顯示當前行、當前列

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命令》一文提到的快速移動光標到行首的效果。

vim按鍵映射


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM