zoukankan      html  css  js  c++  java
  • VIm 一些常用的设置

    一些常用的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 【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.

  • 相关阅读:
    linux:shell:tree
    html5,css3
    asp.net web forms page life cycle
    Unobtrusive Javascript
    Multitier architecture
    C#接口的显示实现和隐式实现
    Modernizr
    android w700
    debian e42 wifi
    mstest run dll
  • 原文地址:https://www.cnblogs.com/xiexiang168/p/5847675.html
Copyright © 2011-2022 走看看