vim:一些命令和选项 filetype, autocmd, map,tabstop等

来源:互联网 发布:矩阵制组织结构的特点 编辑:程序博客网 时间:2024/06/10 21:36

1.filetype

Filetypes *filetypes* *file-types*
Vim can detect the type of file that is edited. This is done by checking the file name and sometimes by inspecting the contents of the file for specific
text.
*:filetype* *:filet*
To enable file type detection, use this command in your vimrc: >
:filetype on
Each time a new or existing file is edited, Vim will try to recognize the type of the file andset the 'filetype' option. This will trigger theFileType
event, which can be used to set the syntax highlighting, set options, etc.
Detail: The ":filetype on" command will load one of these files:
Amiga $VIMRUNTIME/filetype.vim
Mac $VIMRUNTIME:filetype.vim
MS-DOS $VIMRUNTIME\filetype.vim
RiscOS Vim:Filetype
Unix $VIMRUNTIME/filetype.vim
VMS $VIMRUNTIME/filetype.vim
This file is a Vim script that defines autocommands for theBufNewFile and BufRead events. If the file type is not found by the
name, the file $VIMRUNTIME/scripts.vim is used to detect it from the contents of the file.

You can enableloading the plugin files for specific file types with: >
:filetype plugin on
If filetype detection was not switched on yet, it will be as well.This actually loads the file "ftplugin.vim" in 'runtimepath'.The result is that when a file is edited its plugin file is loaded(if there is one for the detected filetype). |filetype-plugin|
*:filetype-plugin-off*
You can disable it again with: >
:filetype plugin off
The filetype detection is not switched off then. But if you do switch off filetype detection, the plugins will not be loaded either.This actually loads the file "ftplugof.vim" in 'runtimepath'.

You can enable loading the indent file for specific file types with: >
:filetype indent on
If filetype detection was not switched on yet, it will be as well.This actually loads the file "indent.vim" in 'runtimepath'. The result is that when a file is edited its indent file is loaded (if there is one for the detected filetype). |indent-expression|
*:filetype-indent-off*
You can disable it again with: >
:filetype indent off
The filetype detection is not switched off then.But if you do switch off filetype detection, the indent files will not be loaded either.This actually loads the file "indoff.vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

filetype plugin on那也会 打开filetype,如果filetype都off了, plugin on 有意义么,上面也说不会load

To see the current status, type: >
:filetype
The output looks something like this: >
filetype detection:ON plugin:ON indent:OFF
'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

The file types are also used for syntax highlighting. If the ":syntax on" command is used, the file type detection is installed too. There is no need
to do ":filetype on" after ":syntax on".



使用filetype 需要 set nocompatible


2. map相关命令

转自https://www.douban.com/group/topic/10866937/

我主要讲解一下“n(普通模式)”下的两个绑定命令,等看完之后就对应的明白别的模式下的命令了。 
适用于普通模式的映射命令主要有: 
1. :map 
[语法] :map {lhs} {rhs} |mapmode-nvo| *:map* 
1.1 作用模式: n、v、o (普通、可视和选择、操作符等待) 
1.2 命令格式: 
:map {lhs} {rhs} 
含义: 在:map作用的模式中把键系列 {lhs} 映射为 {rhs},{rhs}可进行映射扫描,也就是可递归映射。 
1.3 举例: 
:map td :tabnew .<cr> 
含义:在其作用模式(普通、可视、操作符)下,输入td等价于输入 :tabnew . <cr>。而普通模式下输入:tabnew . <cr>就是打开当前目录 
如果再定义绑定 :map ts td,就是指在其作用模式下输入ts等价于td,也就是打开当前目录。不过如果没有特殊需要,一般不建议递归映射。 

2. :noremap 
:moremap和:map命令相对,作用模式和命令格式都相同,只不过不允许再对{rhs}进行映射扫描,也就是{lhs}定义后的映射就是{rhs}的键序列,不会再对{rhs}键序列重新解释扫描。它一般用于重定义一个命令,当然如果:map不需要递归映射的话,建议试用:noremap 
比如: 
:noremap ts td 
它的意思是在其作用模式下,输入ts就是输入td,但是和:map不同的是,此时td再不会做进一步扫描解释。虽然之前已经定义了td,但是不会对td再做扫描 

5. :nmap 
:nmap是:map的普通模式板,也就是说其绑定的键只作用于普通模式。 
例如: 
:nmap td :tabnew .<cr> 和 :map td :tabnew .<cr> 在普通模式下等效 
6. :nnoremap 
:nnorempa和:nmap的关系和:noremap和:map的关系一样,只是:nmap的非递归版 

另外: 
{rhs} 之前可能显示一个特殊字符: 
* 表示它不可重映射 
& 表示仅脚本的局部映射可以被重映射 
@ 表示缓冲区的局部映射 

特殊参数: 

1. <buffer> 
2. <silent> 
3. <special> 
4. <script> 
5. <expr> 
6. <unique> 
它们必须映射命令的后边,在其他任何参数的前面。 

<buffer>如果这些映射命令的第一个参数是<buffer>,映射将只局限于当前缓冲区(也就是你此时正编辑的文件)内。比如: 
:map <buffer> ,w /a<CR> 
它的意思时在当前缓冲区里定义键绑定,“,w”将在当前缓冲区里查找字符a。同样你可以在其他缓冲区里定义: 
:map <buffer> ,w /b<CR> 
比如我经常打开多个标签(:tabedit),想要在各自标签里定义",w"键绑定,那么你只要在每个标签页里分别定义就可,其作用域也只在各自的标签里。同样要清除这些缓冲区的键绑定也要加上<buffer>参数,比如: 
:unmap <buffer> ,w 
:mapclear <buffer> 

<silent>是指执行键绑定时不在命令行上回显,比如: (这个好像经常看到)
:map <silent> ,w /abcd<CR> 
你在输入,w查找abcd时,命令行上不会显示/abcd,如果没有<silent>参数就会显示出来 
??但是我自己试验的,有没有<silent>,命令行都没显示?
查看帮助 :help <silent>

<special>
一般用于定义特殊键怕有副作用的场合。比如: 
:map <special> <F12> /Header<CR> 

<unique>一般用于定义新的键映射或者缩写命令的同时检查是否该键已经被映射,如果该映射或者缩写已经存在,则该命令会失败 

<expr>.
如果定义新映射的第一个参数是<expr>,那么参数会作为表达式来进行计算,结果使用实际使用的<rhs>,例如: 
inoremap <expr> . InsertDot() 
这可以用来检查光标之前的文本并在一定条件下启动全能 (omni) 补全。 
一个例子: 
let counter = 0 
inoremap <expr> <C-L> ListItem() 
inoremap <expr> <C-R> ListReset() 

func ListItem() 
let g:counter += 1 
return g:counter . '. ' 
endfunc 

func ListReset() 
let g:counter = 0 
return '' 
endfunc 
在插入模式下,CTRL-L插入顺序的列表编号,并返回;CTRL-R复位列表编号到0,并返回空。 


<Leader> mapleader 

mapleader变量对所有map映射命令起效,它的作用是将参数<leader>替换成mapleader变量的值,比如: 
:map <Leader>A oanother line<Esc> 
如果mapleader变量没有设置,则用默认的反斜杠代替,因此这个映射等效于: 
:map \A oanother line<Esc> 
意思时输入\A键时,在下一行输入another line并返回到普通模式。 
如果设置了mapleader变量,比如: 
let mapleader = "," 
那么就等效于: 
:map ,A oanother line<Esc> 

<LocalLeader> maplocalleader 
<LocalLeader>和<Leader>类似,只不过它只作用于缓冲区。 
因此在设置mapleader和maplocalleader时最好区分开,不要出现冲突。 

大体上映射的主要部分已经都提到了,还有很多具体的映射相关的内容可以参见:help map


:map 好像可以列出 定义的mapping
定义几个mapping
:nnoremap <silent> [b :bprevious<CR>
:map ]b :bnext<CR>
然后查看
:map
显示
n [b * :bprevious<CR>
]b :bnext<CR>
n gx <Plug>NetrwBrowseX
n <Plug>NetrwBrowseX * :call netrw#NetrwBrowseX(expand("<cWORD>"),0)<CR>
Press ENTER or type command to continue


3. autocmd





4.tabstop softtabstop shiftwidth expandtab


tabstop 默认是 8?
softtabstop 默认是 0?

expandtab
shiftwidth 是 代码缩进的时候的宽度?
http://www.cnblogs.com/jianyungsun/archive/2011/02/16/1955933.html

4.1 shiftwidth

这个是用于程序中自动缩进所使用的空白长度指示的。一般来说为了保持程序的美观,和下面的参数最好一致。同时它也是符号移位长度的制定者。

4.2 tabstop

Number of spaces that a <Tab> in the file counts for.
定义tab所等同的空格长度,一般来说最好设置成8,因为如果是其它值的话,可能引起文件在打印之类的场合中看起来很别扭。除非你设置了 expandtab模式,也就是把tabs转换成空格,这样的话就不会一起混淆,不过毕竟制表符为8是最常用最普遍的设置,所以一般还是不要改。


4.3 softtabstop

Number of spaces that a <Tab> counts for while performing editing operations, like inserting a <Tab> or using <BS>.
如果我们希望改变程序中的缩进怎么办?shiftwidthtabstop不一样的话,你会发现程序比较难看的。这时候,softtabstop就起作用了。可以从vim的说明中看到,一旦设置了softtabstop的值时,你按下tab键,插入的是空格和tab制表符的混合,具体如何混合取决于你设定的softtabstop,举个例子,如果设定softtabstop=8, 那么按下tab键,插入的就是正常的一个制表符;如果设定 softtabstop=16,那么插入的就是两个制表符;如果softtabstop=12,那么插入的就是一个制表符加上4个空格;如果 softtabstop=4呢?那么一开始,插入的就是4个空格,此时一旦你再按下一次tab,这次的四个空格就会和上次的四个空格组合起来变成一个制表符。换句话说,softtabstop是“逢8空格进1制表符”,前提是你tabstop=8
 

4.4 关于expandtab

举个例子,在多人一起开发项目时,为了使代码风格尽量保持一致,一般不允许在代码使用TAB符,而以4个空格代之。我们可以编辑一个文件,包含下面的内容:
set shiftwidth=4
set expandtab
然后把下面的命令加入到.vimrc中:
autocmd FileType c,cpp set shiftwidth=4 | set expandtab

這邊順便引入 vim help 的 'tabstop' 章節:
'tabstop' 'ts'number (default 8)
local to buffer
Number of spaces that a <Tab> in the file counts for. Also see
:retab command, and '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).

       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.
       2. 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.
       3. 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.
       4. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'.  This should then work (for initial indents
only) for any tabstop setting that people use.  It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' is changed.

'softtabstop' 'sts'number (default 0)
local to buffer
{not in Vi}
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.
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.

原创粉丝点击