Skip to Content
《学习 vi 和 Vim 编辑器》,第 8 版
book

《学习 vi 和 Vim 编辑器》,第 8 版

by Arnold Robbins, Elbert Hannah
May 2025
Beginner to intermediate
548 pages
5h 59m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 vi 和 Vim 编辑器》,第 8 版

第 12 章 Vim 脚本 Vim 脚本

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

对于编辑环境来说,有时仅有自定义设置是不够的。Vim 可以让你在.vimrc文件中定义所有喜欢的设置,但也许你需要更动态或 "及时 "的配置。Vim 脚本可以让你做到这一点。

从检查缓冲区内容到处理意外的外部因素,Vim 的脚本语言都能让你完成复杂的任务,并根据自己的需要做出决定。

如果你有一个 Vim 配置文件(.vimrc、.gvimrc 或两者都有),那么你已经在使用 Vim 编写脚本了,只是你不知道而已。所有 Vim 命令和选项都是脚本的有效输入。此外,正如你所期望的那样,Vim 提供了所有标准的流程控制语句(if…then…elsewhile 等)、变量和函数,这些在任何语言中都很常见。

在本章中,我们将通过一个示例,逐步建立一个脚本。我们将学习一些简单的结构,使用 Vim 的一些内置函数,并研究编写规范、可预测的 Vim 脚本所必须考虑的规则。

您最喜欢的颜色(方案)是什么?

让我们从最简单的配置开始。我们将 自定义我们喜欢的环境配色方案。这很简单,使用的是 Vim 脚本的基础之一,即简单的 Vim 命令。

Vim 提供 17 种自定义颜色 方案。1您可以在.vimrc.gvimrc文件中输入colorscheme 命令,选择并激活一种配色方案。一位作者最喜欢的 "低调 "配色方案是沙漠配色方案:

:colorscheme desert

在配置文件中输入colorscheme ,现在每次使用 Vim 编辑时,都能看到自己喜欢的颜色。

因此,我们的第一个脚本是微不足道的。如果您对配色方案的喜好更为复杂呢?如果你喜欢不止一种配色方案呢?如果一天中的时间与你的喜好相关呢?Vim 脚本可以让您轻松做到这一点。

备注

根据一天中的不同时间选择不同的配色方案似乎有些老套,但也许并不像你想象的那样。就连 Google 也会在一天中改变iGoogle主页的颜色和色调。

条件执行

我们中的一个人喜欢把一天分成四个部分,每个部分都有自己专用的配色方案:

darkblue

午夜至清晨 6 点

morning

上午 6 时至中午

shine

中午至下午 6 时

evening

下午 6 时至午夜

为此,我们将创建一个嵌套的if…then…else… 代码块。这个代码块有几种不同的语法。一种语法比较传统,有明确的语法布局:

if cond expr
  line of vim code
  another line of vim code
  ...
elseif some secondary cond expr
  code for this case
else
  code that runs if none of the cases apply
endif

elseifelse 块是可选的,您可以包含多个elseif 块。Vim 也允许使用这种更简洁和类似 C 语言的结构:

cond ? expr 1 : expr 2

Vim 检查条件cond。如果为真,执行expr 1;否则,执行expr 2

使用 strftime() 函数

既然我们可以有条件地执行代码,那么我们就需要弄清楚现在是一天中的什么时候。Vim 的内置函数可以返回此类信息。strftime() strftime() 接受两个参数,其中第一个参数定义了时间字符串的输出格式。这种格式依赖于系统,不可移植,因此在选择格式时必须谨慎。幸运的是,大多数主流格式都是跨系统通用的。第二个可选参数是自 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

《Asterisk:权威指南》第 5 版

《Asterisk:权威指南》第 5 版

Jim Van Meggelen, Russell Bryant, Leif Madsen
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9798341657885