Merge pull request #25059 from lkxed/20201212-Power-up-your-Linux-terminal-text-editor-with-ed

Translated 20201212 Power up your Linux terminal text editor with ed.md
This commit is contained in:
Xingyu.Wang 2022-03-28 16:20:12 +08:00 committed by GitHub
commit d45a58e8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 200 additions and 201 deletions

View File

@ -1,201 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (lkxed)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Power up your Linux terminal text editor with ed)
[#]: via: (https://opensource.com/article/20/12/gnu-ed)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Power up your Linux terminal text editor with ed
======
This deceptively simple editor empowers the user with a set of control
commands that are easy to learn and use.
![Terminal command prompt on orange background][1]
The GNU `ed` command is a line editor. Its considered the standard Unix text editor because it was the very first text editor for Unix, and so it was (and generally still is) available on any POSIX system. In some ways, its easy to tell that it was the first because, in many ways, its extremely rudimentary. Unlike most other text editors, it doesnt open in a window or screen of its own, and in fact, by default, it doesnt even prompt the user for input. On the other hand, its near lack of any interface can also be a strength. Its a functional editor that can be controlled with short instructions either interactively or through a script.
### Installing ed
If youre running Linux or BSD, you probably already have `ed` installed (GNU `ed` on Linux and BSD `ed` on BSD). Some minimal environments, however, omit `ed`, but its probably available from your distributions software repository or ports tree. MacOS ships with BSD `ed` installed.
### Launching ed
When you launch `ed`, it appears that youve lost your prompt, and possibly that `ed` has stalled. It has not; its just waiting for your instructions:
```
$ ed
```
To tell `ed` to be a little more verbose, you can command it to return a prompt with the `p` command:
```
$ ed
p
?
```
The question mark (`?`) is the default `ed` prompt.
### The buffer
While `ed` is active, you work with whats called a _buffer_. The buffer is a place in memory. Youre not editing a file directly; youre only editing the buffer. Should you exit `ed` without writing your changes to a file on disk, then all changes are lost because they only happened in the buffer. (This may sound familiar to experienced Emacs users accustomed to an initial scratch buffer.)
### Writing text with ed
After launching `ed`, youre in command mode. This means you can issue commands to the editor itself, such as when setting it to display a prompt instead of empty space. You can append text to the current buffer with the `a` command, which is terminated by a solitary dot (`.`) on its own line. For instance, this example adds two lines ("hello world" and "hello ed") to the buffer:
```
?
a
hello world
hello ed
.
```
After a terminating dot, you return to command mode.
### Viewing the buffer
To see whats contained in the buffer, you can type either the line you want to see or `,p` to display all lines.
```
?
1
hello world
2
hello ed
,p
hello world
hello ed
```
### Writing to a file
Assuming youre happy with your text, you can write the buffer to a file with the `w` command followed by the name of the destination file.
```
?
w example.txt
19
```
The number after the write operation indicates the number of characters written to the file.
### Reading a file
You dont have to use `ed` for text entry. You can also just open an existing file into the buffer using the `r` command:
```
?
r myfile.txt
```
Alternatively, you can just launch `ed` followed by the file name you want it to load into the buffer:
```
$ ed myfile.txt
```
### Editing the buffer
The `ed` application is a text editor, so you can affect text in the buffer using a special editing syntax. Users of `sed` or `vim` may find some of its syntax familiar. Assume you have a file loaded in the buffer:
```
$ ed myfile.txt
,p
This is an example document.
There is some text, but not much.
There is some errors, but not much.
```
To change the word "document" to "file" in the first sentence, select the line you want to target (1) and then invoke the search function with `s` followed by your search and replacement terms:
```
?
1
This is an example document.
s/document/file/
1
This is an example file.
```
To target a different line, the process is essentially the same but with a different number:
```
?
3
There is some errors, but not much.
s/is/are/
s/much/many/
```
You can see the edits youve made to the buffer using the `,p` command as usual.
```
This is an example file.
There is some text, but not much.
There are some errors, but not many.
```
Of course, these changes only exist in the buffer. Were you to look at the file outside of `ed`, you would see the original text only:
```
$ cat myfile.txt
This is an example document.
There is some text, but not much.
There is some errors, but not much.
```
To save your changes back into the file, use the `w` command:
```
w myfile.txt
258
```
### Clearing the buffer
To get a new buffer so you can either start a new document or load a new one into a fresh environment, use the `c` command. After issuing `c` to clear the buffer, a print command returns nothing because the buffer has been emptied:
```
c
,p
```
### Quit
To exit your `ed` session, use the `q` command. This doesnt give you a chance to save your buffer, so make sure you save before you use this command.
### Try ed
Theres a lot more `ed` can do, and learning `ed` can afford you great insight into how `sed` and parts of `vim` work. I didnt bother trying to write this article in `ed`, admittedly, and Im not sure its the best tool for text entry in general. However, `ed` is an excellent editor of text, and you can learn it easily by reading its documentation. On a GNU system, use `info ed` to view the manual.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/12/gnu-ed
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/terminal_command_linux_desktop_code.jpg?itok=p5sQ6ODE (Terminal command prompt on orange background)

View File

@ -0,0 +1,200 @@
[#]: collector: (lujun9972)
[#]: translator: (lkxed)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Power up your Linux terminal text editor with ed)
[#]: via: (https://opensource.com/article/20/12/gnu-ed)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
使用 ed 来启动你的 Linux 终端文本编辑器
======
这个看似简单的编辑器为用户提供了许多易于学习和使用的命令。
![Terminal command prompt on orange background][1]
GNU `ed` 命令是一个行编辑器。它被认为是标准的 Unix 文本编辑器,因为它是首个出现在 Unix 的文本编辑器,并且它曾经无处不在,你在任何一个 POSIX 系统中都能找到它(通常来说,你现在也可以)。在某种程度上,你也可以很容易看出来它是第一个文本编辑器,因为它在许多方面的功能都十分基础。和其他大多数的文本编辑器不同,它不会打开一个属于自己的窗口或显示区域,事实上,在默认情况下,它甚至不会提示用户输入文字。从另一个方面来说,它在交互功能上的缺失也可以成为一个优点。它是一个多功能的编辑器,你可以用简短的命令控制它,无论是在交互式的命令行中,还是在编写的 shell 脚本里。
### 安装 ed
如果你正在使用 Linux 或者 BSD 的话,你很可能已经默认安装了 `ed`(在 Linux 上是 GNU 的 `ed`,而在 BSD 上是 BSD 的 `ed`)。但是,一些极简的环境可能没有包括 `ed`,这也没关系,你的发行版的软件仓库中很可能有 `ed` 可供下载。macOS 默认安装了 BSD 的 `ed`
### 启动 ed
当你启动 `ed` 的时候,你的终端提示符不见了,看起来好像是 `ed` 停止运行了。其实它没有,它只是在等待你输入指令而已。
```
$ ed
```
为使 `ed` 显示更详细的信息,你可以输入命令 `p` 让它返回一个提示符:
```
$ ed
p
?
```
这个问号(`?`)是默认的 `ed` 提示符。
### 缓存
`ed` 激活时,你其实是在和一个叫 _<ruby>缓存<rt>buffer</rt></ruby>_ 的东西打交道。缓存是内存中的一块区域。你并不会直接编辑文件,而是在编辑它对应的缓存。当你退出 `ed` 却没有把修改保存到磁盘的文件上时,所有的修改都会丢失,因为它们只在缓存中存在。(这听起来可能很耳熟,如果你是一个习惯了<ruby>初始抓取缓存<rt>initial scratch buffer</rt></ruby>的有经验的 Emacs 用户的话。)
### 使用 `ed` 输入文本
启动 `ed` 后,你处于命令模式。这意味着你可以向编辑器发送指令,比如让它显示一个提示符,而不是空白区域。你可以使用 `a` 命令开始附加文本到当前的缓存,使用一个实心的点 `.` 来终止输入。比如下面的这个例子往缓存中附加了两行文字“hello world” 和 “hello ed”
```
?
a
hello world
hello ed
.
```
使用点 `.` 终止输入后,你将回到命令模式。
### 查看缓存
怎样查看当前缓存里都有什么呢?你可以输入想要查看的行号,也可以使用 `,p` 命令来显示所有的行:
```
?
1
hello world
2
hello ed
,p
hello world
hello ed
```
### 写入文件
如果你现在对文本很满意,你可以使用 `w` 命令把缓存写入到文件中,后面跟上目标文件名:
```
?
w example.txt
19
```
写操作后显示的那个数字代表着写入到文件中的字符数。
### 读取文件
除了使用 `ed` 来读取文本,你也可以使用 `r` 命令把一个已经存在的文件加载到到缓存里:
```
?
r myfile.txt
```
另外,你也可以在启动 `ed` 时,在它后面加上你想要加载到缓冲里的文件名:
```
$ ed myfile.txt
```
### 编辑缓冲
鉴于 `ed` 是一个文本编辑器,你当然可以使用一种特殊的语法来编辑缓存中的文本。使用 `sed``vim` 的用户或许会觉得这个语法很熟悉。假设现在缓存中已经加载了一个文件:
```
$ ed myfile.txt
,p
This is an example document.
There is some text, but not much.
There is some errors, but not much.
```
如果你要把第一句话中的 `document` 修改为 `file`你可以先选择目标行1然后使用 `s` 命令调用搜索函数,后面跟着搜索文本和替换文本:
```
?
1
This is an example document.
s/document/file/
1
This is an example file.
```
如果你要编辑其他行,步骤也是一样的,只需提供一个不同的行号即可:
```
?
3
There is some errors, but not much.
s/is/are/
s/much/many/
```
你可以使用 `,p` 命令来看到你对缓存的历史编辑记录:
```
This is an example file.
There is some text, but not much.
There are some errors, but not many.
```
当然,这些修改只存在于缓存中。你如果在 `ed` 编辑器外查看这个文件,你只会看到原始的文本:
```
$ cat myfile.txt
This is an example document.
There is some text, but not much.
There is some errors, but not much.
```
如果你要把这些修改保存回文件中,使用 `w` 命令即可:
```
w myfile.txt
258
```
### 清空缓存
如果想要得到一个新的缓存,以此来打开一个新的文件,或者把一个新的文件加载到不同的环境中,你可以使用 `c` 命令。使用这个清空缓存后,什么也不会输出,因为缓冲已经是空的了:
```
c
,p
```
### 退出
如果要退出当前的 `ed` 会话,你可以使用 `q` 命令。它并不会给你一个保存缓存的机会,所以你要确保自己在这之前执行了保存操作。
### 尝试一下 ed 吧
`ed` 还可以做到很多事情,学习 `ed` 可以让你知道它和部分的 `vim` 是如何工作的。我并没有尝试使用 `ed` 来写这篇文章,老实说,我也不认为它是通常意义上的最佳文本编辑器。但是,`ed` 仍然是一个出色的编辑器。通过阅读它的文档,你可以很轻松地学会它。在 GNU 系统上,你可以使用 `info ed` 来查看它的操作手册。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/12/gnu-ed
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[lkxed](https://github.com/lkxed)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/terminal_command_linux_desktop_code.jpg?itok=p5sQ6ODE (Terminal command prompt on orange background)