Skip to content

Vim Ex Commands & Functions API

Vim Ex commands and built-in functions covering help, files, substitution, buffers and scripting.

1 class · 8 methods

Ex Commands & Functions

8 methods

Vim Ex 命令与脚本函数集合。

:help topic

打开指定主题的帮助文档。

Parameters

NameTypeDescription
topicstring帮助主题,如 'i_CTRL-N'

Returns

无,打开帮助窗口

Example

vim
:help insert.txt
:help :s
:help i_CTRL-N
:w[rite] [file]

将缓冲区写入文件,可指定文件名。

Parameters

NameTypeDescription
filestring目标文件名(可选)

Returns

无,文件被写入

Example

vim
:w
:w backup.txt
:w! forced.txt
:%s/old/new/g

在整个缓冲区将 old 替换为 new,g 标志全局替换。

Parameters

NameTypeDescription
patternregex匹配模式
replacementstring替换文本
flagsstring标志如 g、c、i

Returns

无,缓冲区被修改

Example

vim
:%s/foo/bar/g
:%s/\<the\>/The/gc
:s/old/new/   " 仅当前行
:!{cmd}

执行外部 shell 命令并显示输出。

Parameters

NameTypeDescription
commandstringshell 命令

Returns

无,显示命令输出

Example

vim
:!ls -la
:!make
:.!date   " 用 date 输出替换当前行
:buffers / :ls

列出当前所有缓冲区及其编号与状态。

Returns

无,输出缓冲区列表

Example

vim
:ls
:buffers
:bd 2   " 删除 2 号缓冲区
len({expr})

返回字符串、列表或字典的长度。

Parameters

NameTypeDescription
list/stringstring|list|dict待测量对象

Returns

整数,长度

Example

vim
echo len("hello")        " 5
echo len([1, 2, 3])     " 3
echo len({'a': 1})      " 1
matchstr(expr, pat)

返回字符串中首次匹配正则的子串,无匹配返回空串。

Parameters

NameTypeDescription
exprstring源字符串
patternregex正则模式

Returns

string,匹配的子串

Example

vim
echo matchstr("hello123world", '\d\+')
" "123"

echo matchstr("price: $42", '\$\d\+')
" "$42"
execute({cmd})

执行 Ex 命令字符串并返回输出。

Parameters

NameTypeDescription
commandstringEx 命令字符串

Returns

string,命令输出

Example

vim
echo execute("ls")
echo execute("echo 'hi'")
let out = execute("buffers")