一些常用的vim設置
以下內容皆來源於網絡,感謝原作者。如果引用出處錯誤,請告知以便修改。
1. vim的幾種模式和按鍵映射
轉載自:【1】
Map是Vim強大的一個重要原因,可以自定義各種快捷鍵,用起來自然得心應手。
vim里最基本的map用法也就是
:map c a
這里把c映射成了a,在map生效的情況下,按下c就等同於按下了a
當然,常用的Ctrl,Shift,Alt自然也是支持的。
令Ctrl+a對應到a
:map <C-a> a
令Alt+a對應到a
:map <A-a> a
令Ctrl+Alt+a對應到a
:map <C-A-a> a
到此,我們已經可以做很多事情了。
但是map命令遠不只這一種,在不同的模式下,同一組按鍵可以被映射到不同的組合上。
Vim的模式眾多,但是一般被提及的也就是這么幾種:
Normal Mode
也就是最一般的普通模式,默認進入vim之后,處於這種模式。
Visual Mode
一般譯作可視模式,在這種模式下選定一些字符、行、多列。
在普通模式下,可以按v進入。
Insert Mode
插入模式,其實就是指處在編輯輸入的狀態。普通模式下,可以按i進入。
Select Mode
在gvim下常用的模式,可以叫作選擇模式吧。用鼠標拖選區域的時候,就進入了選擇模式。
和可視模式不同的是,在這個模式下,選擇完了高亮區域后,敲任何按鍵就直接輸入並替換選擇的文本了。
和windows下的編輯器選定編輯的效果一致。普通模式下,可以按gh進入。
Command-Line/Ex Mode
就叫命令行模式和Ex模式吧。兩者略有不同,普通模式下按冒號(:)進入Command-Line模式,可以輸入各種命令,
使用vim的各種強大功能。普通模式下按Q進入Ex模式,其實就是多行的Command-Line模式。
對於Map,有幾個基本的概念
命令的組合
同Vim下的其他命令一樣,命令的名字往往由好幾段組成。前綴作為命令本身的修飾符,微調命令的效果。
對於map而言,可能有這么幾種前綴
nore
表示非遞歸,見下面的介紹
n
表示在普通模式下生效
v
表示在可視模式下生效
i
表示在插入模式下生效
c
表示在命令行模式下生效
Recursive Mapping
遞歸的映射。其實很好理解,也就是如果鍵a被映射成了b,c又被映射成了a,如果映射是遞歸的,那么c就被映射成了b。
:map a b
:map c a
對於c效果等同於
:map c b
默認的map就是遞歸的。如果遇到[nore]這種前綴,比如:noremap,就表示這種map是非遞歸的。
unmap
unmap后面跟着一個按鍵組合,表示刪除這個映射。
:unmap c
那么在map生效模式下,c不再被映射到a上。
同樣,unmap可以加各種前綴,表示影響到的模式。
mapclear
mapclear直接清除相關模式下的所有映射。
同樣,mapclear可以加各種前綴,表示影響到的模式。
這里列出常用的一些map命令,默認map命令影響到普通模式和可視模式。
:map :noremap :unmap :mapclear
:nmap :nnoremap :nunmap :nmapclear
:vmap :vnoremap :vunmap :vmapclear
:imap :inoremap :iunmap :imapclear
:cmap :cnoremap :cunmap :cmapclear
可以試試這些命令
命令行模式下建一個mapping
nmap b a
現在普通模式下,按b,可以進入插入模式,隨便輸入一些字符
命令行模式下建一個mapping
vmap b d
現在普通模式下,按V,進入了可視模式,並且選定了一整行,按下b,可以刪除整行
命令行模式下建一個mapping
imap b a
現在試着給正在編輯的這個文件輸入一個字符”b”吧 :p
命令行模式下建一個mapping
cmap b c
命令行模式下, 按下b,會出來一個a
好了,到此vim的按鍵已經被你弄得亂七八糟了,試着用unmap和mapclear清除這些mapping吧。:]
Reference
【1】vim的幾種模式和按鍵映射(http://haoxiang.org/2011/09/vim-modes-and-mappin/)
2. 鍵盤映射時<leader>的作用是什么?
The <Leader> key is mapped to \ by default. So if you have a map of <Leader>t, you can execute it by default with \t【1】.
For more detail or re-assigning it using the mapleader variable, see
:help leader
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead.
Example:
:map <Leader>A oanother line <Esc>
Works like:
:map \A oanother line <Esc>
But after:
:let mapleader = ","
It works like:
:map ,A oanother line <Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
Reference
【1】What is the <leader> in a .vimrc file?(http://stackoverflow.com/questions/1764263/what-is-the-leader-in-a-vimrc-file)
3. vim與Tab相關的幾個參數
轉載自【1】:
normal 模式下:
>> 當前行增加縮進
<< 當前行減少縮進
insert模式下:
CTRL+SHIFT+T:當前行增加縮進(Ubuntu下試過,不對)
CTRL+SHIFT+D:當前行減少縮進
與Tab相關的參數有shiftwidth、tabstop、softtabstop、expandtab。
shiftwidth 縮進操作(<<和>>)時縮進的列數(這里的一列相當於一個空格)
tabstop 一個tab鍵所占的列數,linux 內核代碼建議每個tab占用8列
softtabstop 敲入tab鍵時實際占有的列數。
expandtab 輸入tab時自動將其轉化為空格
-
softtabstop大於tabstop時,且沒有設置expandtab時,例如:softtabstop=12,tabstop=8,那么當輸入一個tab時(softtabstop:實際占用的是12列),最后會變成一個tab(tabstop)加4個空格(8+4),輸入兩個tab(2個softtabstop:24列)會變成3個tab(tabstop),也就是說vim或用tabstop+空格來表示,最終你能看到的縮進的列數一定是softtabstop*按的tab鍵次數。(ps:
:set list
可以查看tab符號) -
softtabstop小於tabstop且沒有設置expandtab時,如果softtabstop=4,tabstop=8,輸入一個tab(softtabstop),會變成4個空格(因為不夠用一個tabstop表示),輸入兩個tab會變成一個tab(8列)。
-
如果softtabstop等於tabstop,而且expandtab沒有設置,softtabstop與tabstop就沒什么區別了。
-
如果設置的expandtab,輸入一個tab,將被展開成softtabstop值個空格,如果softtabstop=4,那么一個tab就會被替換成4個空格。
常用的tab設置為:
" 設置tab縮進為空格
set expandtab "set et
" 設置換行自動縮進為4個空格
set shiftwidth=4
" 設置tab鍵縮進為4個空格的距離,vim默認是8
set tabstop=4 "set ts=4
" 敲入tab鍵時,一個實際占有的列數
set softtabstop=4
Reference
【1】每日一Vim(9)----縮進(http://liuzhijun.iteye.com/blog/1831548)
4. vim檢測文件類型
執行:filetype可以查看Vim的文件類型檢測功能是否已打開,默認你會看到:detection:ON plugin:OFF indent:OFF。
下面詳細介紹這三個參數的具體含義【1】。
detection:默認情況vim會對文件自動檢測文件類型,也就是你看到的'detection:ON',同樣你可以手動關閉:filetype off。 可以用:set filetype查看當前文件是什么類型了。 類似file.txt文件的filetype設置為python,那么就和普通的python文件一樣的顯示效果了:set filetype=python。
另一種方式就是在文件內容中指定,Vim會從文件的頭幾行自動掃描文件是否有聲明文件的類型的代碼,如在文件的行首加入# vim: filetype=python,Java文件變通的做法/* vim: filetype=java */,總之就是把這行當作注釋,以致於不影響文件的編譯,這樣Vim不通過文件名也能檢測出文件是什么類型了。
plugin:如果plugin狀態時ON,那么就會在Vim的運行時環境目錄下加載該類型相關的插件。比如為了讓Vim更好的支持Python編程,你就需要下載一些Python相關的插件,此時就必須設置plugin為ON插件才會生效,具體設置方法就是:filetype plugin on
indent:不同類型文件有不同的方式,比如Python就要求使用4個空格作為縮進,而c使用兩個tab作為縮進,那么indent就可以為不同文件類型選擇合適的縮進方式了。你可以在Vim的安裝目錄的indent目錄下看到定義了很多縮進相關的腳本。具體設置方法:filetype indent on。
以上三個參數,可以寫成一行filetype plugin indent on設置在_vimrc文件中。
Reference
【1】每日一Vim(25)filetype---- 文件類型檢測(http://liuzhijun.iteye.com/blog/1846123)
5. vim自動縮進的幾種方式
There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like 'autoindent' and 'smartindent', to complex ones such as 'cindent' and custom indentation based on filetype using 'indentexpr'. The amount of indentation used for one level is controlled by the 'shiftwidth' option(縮進的個數是由'shiftwidth'設置來控制的).【1】
'autoindent'
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.
'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.(一些基於filetype來縮進的腳本會自動enable它,所以set autoindent 加上filetype plugin indent on是一個比較好的選擇)
'smartindent' and 'cindent'
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.
Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.(僅當你對基於文件類型的縮進不滿意時,才需要設置'smartindent' or 'cindent')
參照【2】的介紹,
smartindent is an old script that was meant, when it was written, to be a "smart" complement to autoindent. Since then, most languages have either specific indentation functions or use cindent with specific options.
Generally, smartindent shouldn't be used at all.
The following lines are usually enough to deal with indentation(通常,如下設置便足夠了):
set autoindent
filetype plugin indent on
Reference
【1】Methods for automatic indentation(http://vim.wikia.com/wiki/Indenting_source_code)
【2】autoindent is subset of smartindent in vim?(http://stackoverflow.com/questions/18415492/autoindent-is-subset-of-smartindent-in-vim)
6. vim粘貼代碼格式變亂
有時候從編輯器里面復制粘貼代碼到vim中,代碼格式會完全亂套。其原因是vim開啟了smartindent(智能縮減)或autoindent(自動對齊)模式。為了保持代碼的格式,在粘貼前可以先停止上面的兩種模式,在Normal模式下的命令為【1】:
:set nosmartindent :set noautoindent
為了一個粘貼搞出這么多事來,確實是麻煩。不過還有一個更加簡單的方法,用命令開始粘貼模式,即:
(1)開啟 :set paste (2)按i進入插入模式,然后執行粘貼操作
為什么要先進入插入模式再粘貼呢?因為你所要粘貼的內容如果含有字符i的話,在Normal模式下,字符i會被Vim看做是插入命令,i字符前面的內容會被丟失(比如復制 #include <stdio.h>,復制的結果為 nclude <stdio.h>)。
(3)關閉
:set nopaste 或 :set paste!
由於粘貼模式和上面的smartindent、autoindent模式是互斥的,而smartindent、autoindent是不可少的,所以粘貼完后使用上面的兩條命令之一來關閉粘貼模式。
另外還可以通過綁定自定義快捷鍵的方式來快速切換,例如將下屬配置加入到.vimrc中
方式1: set pastetoggle=<F4> 方式2: :map <F8> :set paste :map <F9> :set nopaste
注意:方式1在閱讀和編輯模式下都可以使用,對粘貼模式開啟和關閉進行切換;方式2是在閱讀模式下使用,按下相應的快捷鍵就相當於執行后面定義的命令。
Reference
【1】vim粘貼代碼格式變亂(http://www.netingcn.com/vim-paste-mode.html)
7. vim折疊設置
1. 折疊方式【1】
可用選項 'foldmethod' 來設定折疊方式:set fdm=*****。
有 6 種方法來選定折疊:
manual 手工定義折疊
indent 更多的縮進表示更高級別的折疊
expr 用表達式來定義折疊
syntax 用語法高亮來定義折疊
diff 對沒有更改的文本進行折疊
marker 對文中的標志折疊
注意,每一種折疊方式不兼容,如不能即用expr又用marker方式,我主要輪流使用indent和marker方式進行折疊。
使用時,用:set fdm=marker 命令來設置成marker折疊方式(fdm是foldmethod的縮寫)。
要使每次打開vim時折疊都生效,則在.vimrc文件中添加設置,如添加:set fdm=syntax,就像添加其它的初始化設置一樣。
2. 折疊命令
zi / za 打開關閉折疊
zv 查看此行
zm 關閉折疊
zM 關閉所有
zr 打開
zR 打開所有
zc 折疊當前行
zo 打開當前折疊
zd 刪除折疊
zD 刪除所有折疊
3. 如何在打開文件時默認不折疊?【2】
set
foldlevel=99
Should open all folds, regardless of method used for folding. With foldlevel=0
all folded, foldlevel=1
only somes, ... higher numbers will close fewer folds.