一些经常使用的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

相关文章
相关标签/搜索