一些經常使用的vim設置

 如下內容皆來源於網絡,感謝原做者。若是引用出處錯誤,請告知以便修改。html

1. vim的幾種模式和按鍵映射

轉載自:【1】java

Map是Vim強大的一個重要緣由,能夠自定義各類快捷鍵,用起來天然駕輕就熟。
vim裏最基本的map用法也就是python

:map c a
這裏把c映射成了a,在map生效的狀況下,按下c就等同於按下了a
固然,經常使用的Ctrl,Shift,Alt天然也是支持的。linux

令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以後,處於這種模式。vim

Visual Mode
通常譯做可視模式,在這種模式下選定一些字符、行、多列。
在普通模式下,能夠按v進入。windows

Insert Mode
插入模式,其實就是指處在編輯輸入的狀態。普通模式下,能夠按i進入。bash

Select Mode
在gvim下經常使用的模式,能夠叫做選擇模式吧。用鼠標拖選區域的時候,就進入了選擇模式。
和可視模式不一樣的是,在這個模式下,選擇完了高亮區域後,敲任何按鍵就直接輸入並替換選擇的文本了。
和windows下的編輯器選定編輯的效果一致。普通模式下,能夠按gh進入。網絡

Command-Line/Ex Mode
就叫命令行模式和Ex模式吧。二者略有不一樣,普通模式下按冒號(:)進入Command-Line模式,能夠輸入各類命令,
使用vim的各類強大功能。普通模式下按Q進入Ex模式,其實就是多行的Command-Line模式。app

對於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時自動將其轉化爲空格
  1. 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符號)

  2. softtabstop小於tabstop且沒有設置expandtab時,若是softtabstop=4,tabstop=8,輸入一個tab(softtabstop),會變成4個空格(由於不夠用一個tabstop表示),輸入兩個tab會變成一個tab(8列)。

  3. 若是softtabstop等於tabstop,並且expandtab沒有設置,softtabstop與tabstop就沒什麼區別了。

  4. 若是設置的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】

setfoldlevel=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.

Reference

【1】vim摺疊設置(http://www.cnblogs.com/welkinwalker/archive/2011/05/30/2063587.html)

【2】How to set the default to unfolded when you open a file?(http://stackoverflow.com/questions/8316139/how-to-set-the-default-to-unfolded-when-you-open-a-file)

【3】http://stevelosh.com/blog/2010/09/coming-home-to-vim/#using-the-leader

相關文章
相關標籤/搜索