vim configuration file syntax

Abstract: In the process of vim configuration, that is, in the process of writing vimrc files, it is very important to be familiar with the syntax of vim configuration files. This article mainly explains the relevant syntax of vimrc, and prepares for configuring vimrc as needed in the future.

1. Notes

Before writing a program, the first thing is not to know the syntax, but to know how to write comments.
Comments in vimrc scripts are line comments using quotation marks (").

2. Variables

(1) Scalar variables
can be numbers or strings, basically the same as perl.
The naming method is: scope: variable name, commonly used are as follows:
b:name —— variable only valid for the current buffer
w:name —— variable only valid for the current window
g:name —— global variable
v: name —— vim predefined variable
a:name —— parameter variable of function
Note: Please include scope and colon when referring to scalar variable

(2) A type of variable naming method with special meaning
: Fun Character (for this word, please refer to Programming There are three types of Perl) plus variable names
:
$NAME - environment variables (generally variable names are capitalized)
&name - options (default settings when vim handles certain things)
@r - register (register, not assembly ) EAX, EBX, see Part 2 vim tips)
Examples of common environment variables: $VIMRUNTIME -
common options for the vim run path: &ic - ignorecase
Note: Use the set command to change option settings, for example:
:set ignorecase
use a set command to see all current options and their settings.

(3) Variable assignment
: let variable name=value
Note: The first colon is not only to indicate that this is a colon command, but also necessary.

Release variables: :unlet! variable name

(4) operator (basically the same as perl)
数学运算:+ - * / % .
逻辑运算:== != > >= < <= ?:
正则匹配运算符=~ !~

3. 控制结构

(1) if 条件
语句块
elseif 条件
语句块
else
语句块
endif
注意:条件表达式不需要小括号,语句块不需要大括号

(2) while 条件
语句块
[break/continue]
endwhile

4. 函数:

定义:
function 函数名(参数)
函数体
endfunc

调用:
在脚本语句中使用 call 函数名(参数)
在vim命令中使用 :call 函数名(参数)

注:在函数体中使用参数需要在参数变量名称前加上a:,例如参数名为
keyword,
则函数体中使用a:keyword来引用

注:常用系统函数 参见【附】。

5. 执行命令,键盘绑定,命令行命令和自动命令

(1) 执行命令
exec "命令" —— 用于在vim脚本中执行一系列vim命令
:!外部命令 —— 这是一个vim命令行命令,功能是调用外部程序
(2) 键盘绑定 :help map-overview
vim最大的特点在于可以把所有的操作能够用一个命令字符串表达出来,
因此这带来了编写脚本的最大的便利。键盘绑定就是一个例子,这个功能允许
把一个命令字符串绑定到一个按键/按键组合。

一般格式:映射命令 按键组合 命令组合
例子:nmap c ^i#<Esc>j
解释:映射normal模式下的按键c为:^i#<Esc>j,就是在该行开头加上#号
,然后下移一行

常用映射命令:
map :全模式映射
nmap :normal模式映射
vmap :visual模式映射
imap :insert模式映射

(3) 命令行命令
vim支持在启动的时候使用-c开关执行命令字符串,例如:
$ cat n
#!/bin/sh
vim -c "set filetype=$PERL" -c "s.$.#!/usr/bin/$PERL -w"r"r." -c
":nohlsearch" $1
设置文件类型 写入#!/usr/bin/perl -w
取消匹配加亮

$ n myperlfile


(4) 自动命令
一般格式:autocmd 事件 文件类型 命令
例子:au BufNewFile,BufRead *.pl setf perl
解释:当事件 BufNewFile 和 BufRead 发生在 *.pl 文件上的时候,

执行命令:setf perl


6.其他

runtime/runtimepath

:ru[ntime][!] {file} ..
                        Read Ex commands from {file} in each directory given
                        by 'runtimepath'.  There is no error for non-existing
                        files.  Example: >
                                :runtime syntax/c.vim

解析:无论我们在vimrc在看到别人使用了哪个命令,只需要输入:cmd,就能找到这个命令的帮助说明


vim的更多进阶使用,请移步:http://easwy.com/blog/archives/advanced-vim-skills-catalog/

<leader> 的作用

85"Set mapleader
 86 let mapleader = ","
 87 let g:mapleader = ","
 88  
 89 "Fast saving
 90 nmap <leader>w :w!<cr>
 91 nmap <leader>f :find<cr>

<leader> 表示命令模式下的 "引导键" , 设置之后 连续按下 “,w” 就映射到了保存,并且不用按空格
更多配置方面的参考:http://www.douban.com/note/174206360/

7.常见键盘键的表述

:help keycodes
在进行键盘映射的时候,如果你想知道<cr> <up> <lt>等等分别表示什么意思,输入:help keycodes 即可。



转自:http://blog.csdn.net/trochiluses/article/details/21776365

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734122&siteId=291194637