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 版

第 16 章 vi 无处不在

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

导言

我们已经介绍了使vi 和 Vim 成为强大编辑器的许多功能。但vi 不仅仅是一个编辑器,它还是一种哲学。 它是一种哲学。 它是一种以不同方式思考文字的方法。 它让我们将文本视为对象。 这些对象一旦学会,就会形成一种与 "点击 "和 "所见即所得"(WYSIWYG)截然不同的编辑方法。 文本即对象是一种有趣的抽象概念,它如此流行,以至于影响到了其他工具,其中有些可能会让你大吃一惊。 本章将介绍vi-think 的一些常见实例和一些不太常见(但却非常有用)的实例。

改善命令行体验

正如vi 用户是强大的用户一样,他们的 "强大 "可以超越文本编辑。 多年来,命令行工具(终端模拟器、DOS 窗口等)提供了基本的命令行编辑和历史记录功能。 越来越多的开放源码贡献为命令行环境带来了巨大的改进。vi 是许多命令行环境中比较流行的命令行历史记录管理实现之一。

在 Unix 中,命令行被称为shell。 有许多 shell。其中最流行的有sh (原始的 Bourne shell)、Bash(GNU Bourne-again shell)、 csh (C shell)、1ksh (Korn shell)和zsh(Z shell)。

大多数但并非所有的现代 shell 都提供vi-mode 命令行编辑功能,我们将看到这一点。

共享多个外壳

警告

在测试我们将要介绍的内容之前, ,我们强烈建议您按照我们将要给出的指示进行操作。 我们没有这样做,因此丢失了包含近 8000 条存储历史命令的历史文件!

在下面的示例中,我们将简要介绍启用命令历史编辑所需的选项,以及如何通过vi 按键浏览命令历史。 由于需要调用不同的 shell 来测试不同的选项,因此创建的 shell 实例都有自己的 "环境 "概念,即每个 shell 特有的变量和行为。 不过,有些 shell 有历史文件的默认值,当你启动或调用它们时,它们不会去覆盖已有的历史文件定义。

例如,如果你经常使用zsh 并调用不同的 shell (ksh),这样做并不会改变历史文件变量 (HISTFILE) 的值,尽职地将ksh 命令记录到zsh 历史文件中。 当你存在ksh 时,现有的zsh 就会茫然不知所措,而且历史文件也会损坏!虽然这并不是世界末日,但如果你想获得历史的力量,就不要让这种情况发生在你身上! 因此,你应该这样做:

  1. 在你的主目录下,为每种 shell 创建或编辑一个启动文件:ksh (.kshrc), bash (.bashrc), 和zsh (.zshrc). 确保不要覆盖任何已有的此类文件。

  2. 在每个启动文件中,通过添加或验证这些行是否存在,确保不会丢失任何有价值的历史数据:

# make BACKSPACE key do what it should do
stty sane

# set command-line editing to vi mode.
set -o vi

# keep history files in a hidden folder please.
myhistorydir=${HOME}/.history
# make the directory, fail silently if it's already there
mkdir -p ${myhistorydir}

# save lots of commands. ...
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