Ex Commands & Functions
8 methodsVim Ex 命令与脚本函数集合。
:help topic打开指定主题的帮助文档。
Parameters
| Name | Type | Description |
|---|---|---|
| topic | string | 帮助主题,如 'i_CTRL-N' |
Returns
无,打开帮助窗口
Example
vim
:help insert.txt
:help :s
:help i_CTRL-N:w[rite] [file]将缓冲区写入文件,可指定文件名。
Parameters
| Name | Type | Description |
|---|---|---|
| file | string | 目标文件名(可选) |
Returns
无,文件被写入
Example
vim
:w
:w backup.txt
:w! forced.txt:%s/old/new/g在整个缓冲区将 old 替换为 new,g 标志全局替换。
Parameters
| Name | Type | Description |
|---|---|---|
| pattern | regex | 匹配模式 |
| replacement | string | 替换文本 |
| flags | string | 标志如 g、c、i |
Returns
无,缓冲区被修改
Example
vim
:%s/foo/bar/g
:%s/\<the\>/The/gc
:s/old/new/ " 仅当前行:!{cmd}执行外部 shell 命令并显示输出。
Parameters
| Name | Type | Description |
|---|---|---|
| command | string | shell 命令 |
Returns
无,显示命令输出
Example
vim
:!ls -la
:!make
:.!date " 用 date 输出替换当前行:buffers / :ls列出当前所有缓冲区及其编号与状态。
Returns
无,输出缓冲区列表
Example
vim
:ls
:buffers
:bd 2 " 删除 2 号缓冲区len({expr})返回字符串、列表或字典的长度。
Parameters
| Name | Type | Description |
|---|---|---|
| list/string | string|list|dict | 待测量对象 |
Returns
整数,长度
Example
vim
echo len("hello") " 5
echo len([1, 2, 3]) " 3
echo len({'a': 1}) " 1matchstr(expr, pat)返回字符串中首次匹配正则的子串,无匹配返回空串。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | 源字符串 |
| pattern | regex | 正则模式 |
Returns
string,匹配的子串
Example
vim
echo matchstr("hello123world", '\d\+')
" "123"
echo matchstr("price: $42", '\$\d\+')
" "$42"execute({cmd})执行 Ex 命令字符串并返回输出。
Parameters
| Name | Type | Description |
|---|---|---|
| command | string | Ex 命令字符串 |
Returns
string,命令输出
Example
vim
echo execute("ls")
echo execute("echo 'hi'")
let out = execute("buffers")