前言
- 常常在不同環境用到不太一樣設定的 vim,決定統整一版順手的基本設定,可依照情況再自己調整。
設定
先寫在 ~/.vimrc 檔案裡
總之,先開啟 ~/.vimrc
:
常用設定
以下是我自己喜歡的常用設定;因為最近剛好常寫 yml
檔案,所以改成 2
,一般程式碼可以用 4
:
1
2
3
4
5
6
7
|
set tabstop=2
set shiftwidth=2
set softtabstop=-1
set expandtab
set hlsearch!
nnoremap <CR> :noh<CR><CR>
|
簡單說明:
tabstop
:文件中 Tab 會代表空幾格
shiftwidth
:按下 Tab 鍵要空幾格
softtabstop
:換行的時候會自動幫你空多少 Tab;但要視情況而定,例如是後括號,就不一定會幫你自動空格。
expandtab
:把 Tab 符號自動轉成 space (空白),也就是文件裡面不會有 \t
hlsearch
:hightlight search,會把搜尋的字詞標成黃色,方便查找
nnoremap <CR> :noh<CR><CR>
:當每次按下 <Enter>
鍵時,會自動帶上 :noh
(取消 hightlight 標示) 後,再補上原有的 <Enter>
功能。
無腦原則
- 把
tabstop
、shiftwidth
、softtabstop
設定成一樣的值,推薦可以是 2
或 4
- 如果不喜歡有 tab 符號,就設定
expandtab
(尤其是編輯 Python script 時);如果喜歡就保留預設值 (noexpandtab
)
- 如果覺得每次搜尋的結果都要手動輸入
:noh
很麻煩,就輸入 nnoremap <CR> :noh<CR><CR>
;喜歡自己控制就不要寫。
如果想要再配合自動 indent,可以再加上
1
2
|
set autoindent
set smartindent
|
詳細說明文件
- 以下內容都可以在
vim
中輸入 :help <參數>
來查到結果。
- e.g.
tabstop
(ts
)
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
|
*'tabstop'* *'ts'*
'tabstop' 'ts' number (default 8)
local to buffer
Number of spaces that a <Tab> in the file counts for. Also see
the |:retab| command, and the 'softtabstop' option.
Note: Setting 'tabstop' to any other value than 8 can make your file
appear wrong in many places, e.g., when printing it.
The value must be more than 0 and less than 10000.
There are four main ways to use tabs in Vim:
1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
(or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim
will use a mix of tabs and spaces, but typing <Tab> and <BS> will
behave like a tab appears every 4 (or 3) characters.
This is the recommended way, the file will look the same with other
tools and when listing it in a terminal.
2. Set 'softtabstop' and 'shiftwidth' to whatever you prefer and use
'expandtab'. This way you will always insert spaces. The
formatting will never be messed up when 'tabstop' is changed (leave
it at 8 just in case). The file will be a bit larger.
You do need to check if no Tabs exist in the file. You can get rid
of them by first setting 'expandtab' and using `%retab!`, making
sure the value of 'tabstop' is set correctly.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
'expandtab'. This way you will always insert spaces. The
formatting will never be messed up when 'tabstop' is changed.
You do need to check if no Tabs exist in the file, just like in the
item just above.
4. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
|modeline| to set these values when editing the file again. Only
works when using Vim to edit the file, other tools assume a tabstop
|
大意:
- 文件中的所有 Tab (
\t
) 「看起來」要代表多少 space
- 不要設定超過
8
不然會容易跑版
shiftwidth
(sw
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
shiftwidth([{col}]) *shiftwidth()*
Returns the effective value of 'shiftwidth'. This is the
'shiftwidth' value unless it is zero, in which case it is the
'tabstop' value. This function was introduced with patch
7.3.694 in 2012, everybody should have it by now (however it
did not allow for the optional {col} argument until 8.1.542).
When there is one argument {col} this is used as column number
for which to return the 'shiftwidth' value. This matters for the
'vartabstop' feature. If the 'vartabstop' setting is enabled and
no {col} argument is given, column 1 will be assumed.
Can also be used as a |method|:
GetColumn()->shiftwidth()
|
softtabstop
(sts
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
'softtabstop' 'sts' number (default 0)
local to buffer
Number of spaces that a <Tab> counts for while performing editing
operations, like inserting a <Tab> or using <BS>. It "feels" like
<Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
used. This is useful to keep the 'ts' setting at its standard value
of 8, while being able to edit like it is set to 'sts'. However,
commands like "x" still work on the actual characters.
When 'sts' is zero, this feature is off.
When 'sts' is negative, the value of 'shiftwidth' is used.
'softtabstop' is set to 0 when the 'paste' option is set and restored
when 'paste' is reset.
See also |ins-expandtab|. When 'expandtab' is not set, the number of
spaces is minimized by using <Tab>s.
The 'L' flag in 'cpoptions' changes how tabs are used when 'list' is
set.
NOTE: This option is set to 0 when 'compatible' is set.
If Vim is compiled with the |+vartabs| feature then the value of
'softtabstop' will be ignored if |'varsofttabstop'| is set to
anything other than an empty string.
|
- 在做一些編輯動作時,Tab 要自動補上的數量;設定成
0
是關閉、設定成 -1
則是強迫與 shiftwidth
值相同。
expandtab
(et
)
1
2
3
4
5
6
7
8
9
10
|
*'expandtab'* *'et'* *'noexpandtab'* *'noet'*
'expandtab' 'et' boolean (default off)
local to buffer
In Insert mode: Use the appropriate number of spaces to insert a
<Tab>. Spaces are used in indents with the '>' and '<' commands and
when 'autoindent' is on. To insert a real tab when 'expandtab' is
on, use CTRL-V<Tab>. See also |:retab| and |ins-expandtab|.
This option is reset when the 'paste' option is set and restored when
the 'paste' option is reset.
NOTE: This option is reset when 'compatible' is set.
|
- 在 insert mode 時,用適當的 space 數量替代 Tab。
hlsearch
(hls
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
*'hlsearch'* *'hls'* *'nohlsearch'* *'nohls'*
'hlsearch' 'hls' boolean (default off)
global
{not available when compiled without the
|+extra_search| feature}
When there is a previous search pattern, highlight all its matches.
The type of highlighting used can be set with the 'l' occasion in the
'highlight' option. This uses the "Search" highlight group by
default. Note that only the matching text is highlighted, any offsets
are not applied.
See also: 'incsearch' and |:match|.
When you get bored looking at the highlighted matches, you can turn it
off with |:nohlsearch|. This does not change the option value, as
soon as you use a search command, the highlighting comes back.
'redrawtime' specifies the maximum time spent on finding matches.
When the search pattern can match an end-of-line, Vim will try to
highlight all of the matched text. However, this depends on where the
search starts. This will be the first line in the window or the first
line below a closed fold. A match in a previous line which is not
drawn may not continue in a newly drawn line.
You can specify whether the highlight status is restored on startup
with the 'h' flag in 'viminfo' |viminfo-h|.
NOTE: This option is reset when 'compatible' is set.
|
nnoremap
1
2
3
4
5
6
7
|
'remap' boolean (default on)
global
Allows for mappings to work recursively. If you do not want this for
a single entry, use the :noremap[!] command.
NOTE: To avoid portability problems with Vim scripts, always keep
this option at the default "on". Only switch it off when working with
old Vi scripts.
|
remap
代表會按鍵對應 (key mapping) 會遞迴套用 (recursively)
noremap
則代表不要遞迴套用 (no-recursively-map)
- 第一個
n
則代表 mode:vim 有很多 mode,例如 insert mode、visual mode;這裡的 n
表示 normal mode。
REF