这个vim中的一系列按键操作如何删除除编号行以外的所有内容?

4

我正在完成这个仅限中间部分的编程挑战,

Leave only the
numbered lines.
LINE 1
LINE 2
LINE 3
That's all.
Thank you
very much.

以下按键序列在缓冲区中有意义并会有所作用:
djGd2kZZ

这基本上是将命令链接在一起。但是以下命令究竟在做什么,为什么我看不到它在缓冲区中?我尝试省略 "q!"(退出?)命令,但是那样它就无法工作。

)3:wq!<CR>
1个回答

5

)是一个动作, 它可以跳转到下一句话。一句话的定义为:

*sentence*

    A sentence is defined as ending at a '.', '!' or '?' followed by either the
    end of a line, or by a space or tab.

在这个文件中:
Leave only the
numbered lines.
LINE1

第一行和第二行是以句点和换行符结尾的句子,所以)将光标移动到LINE 1的开头。 :wq!<CR>是保存并退出的常见序列,无需提示即可覆盖。
诀窍在于:w接受范围命令
:[range]w[rite][!] [++opt]

    Write the specified lines to the current file.  This
    is unusual, because the file will not contain all
    lines in the buffer.

: 前面放置一个数字是 N: 计数和范围

Count and Range                     *N:*

    When giving a count before entering ":", this is translated into:
        :.,.+(count - 1)

    In words: The 'count' lines at and after the cursor.  Example: To delete
    three lines:
        3:d<CR>    is translated into: .,.+2d<CR>

3:w 变成 :.,.+2w,意思是写入当前行和接下来的两行。

)3:wq!<CR> 并不是删除除了数字行以外的所有内容,而是将数字行保存到原始文件中。当Vim强制退出时,其余文本将丢失。这就是为什么您看不到缓冲区更改的原因 - 它不会更改。

(过了一段时间,我自己也用 djGd2kZZ 解决了这个问题,并不知道您可以使用 [range]:w 直到我看到了更短的答案)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接