在 vi 中使用通配符编辑多个文件

发布于 2024-09-01 18:53:16 字数 514 浏览 4 评论 0原文

当使用程序员文本编辑器 vi 时,我经常使用通配符搜索来懒惰地搜索我想要编辑的文件。

vi ThisIsAReallLongFi*.txt

当这与单个文件匹配时,效果很好。然而,如果它匹配多个文件 vi 会做一些奇怪的事情。

首先,它打开第一个文件进行编辑

其次,当我 :wq 退出文件时,我在终端底部收到一条消息,如下所示

E173: 4 more files to edit
Hit ENTER or type command to continue

当我按 Enter 键时,它使我返回到文件中的编辑模式我刚刚进去。我期望的行为是 vi 将继续编辑下一个文件。

那么,

  1. 此处 vi 行为背后的逻辑是什么

  2. 有办法继续并编辑下一个已匹配的文件吗?

是的,我知道制表符补全,这个问题是基于好奇心并希望更好地了解 shell。

When using the programmers text editor vi, I'll often use a wildcard search to be lazy about the file I want to edit

vi ThisIsAReallLongFi*.txt

When this matches a single file it works great. However, if it matches multiple files vi does something weird.

First, it opens the first file for editing

Second, when I :wq out of the file, I get a message the bottom of the terminal that looks like this

E173: 4 more files to edit
Hit ENTER or type command to continue

When I hit enter, it returns me to edit mode in the file I was just in. The behavior I'd expect here would be that vi would move on to the next file to edit.

So,

  1. What's the logic behind vi's behavior here

  2. Is there a way to move on and edit the next file that's been matched?

And yes, I know about tab completion, this question is based on curiosity and wanting to understand the shell better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-09-08 18:53:16

vi 支持多个文件可供编辑。 :n 转到下一个文件,:N 转到上一个文件。使用 :h arglist 获取更多信息。

vi supports having multiple files available for editing. :n goes to the next file, :N goes to the previous. Use :h arglist for more information.

抱猫软卧 2024-09-08 18:53:16

当前问题的答案

您可以编写:vim -p myfile*,vim 会将当前目录中 myfile* 的所有匹配项打开到多个选项卡中。然后,您可以一一编辑所有文件。使用 gt 导航前往下一个选项卡,使用 gT 导航前往上一个选项卡。要一次性保存并退出所有文件,只需在 vim 中写入 :wqa 即可。

类似问题的答案

我也面临着类似的问题。我当前目录的多个子目录中有一个名为“myfile*”的文件。我想对所有这些内容进行一个小的更改,而不需要一一打开它们。所以,在命令行中,我这样写:

$find . -type f -name "myfile*" -exec vim -f {} \;

这样,find 为每个文件运行 vim。一旦我退出 vim 实例, find 就会自动运行另一个文件并处理下一个文件,直到所有文件都完成为止。尽管如此,我同意如果所有文件都可以作为选项卡打开会更好。

ANSWER TO CURRENT QUESTION

You may write: vim -p myfile* and vim will open all the matches for myfile* in the current directory into multiple tabs. Then, you can edit all files one by one. Navigate using gt for going to the next tab and gT for going to the previous tab. For saving and quiting all the files in a single go, just write :wqa inside vim.

ANSWER TO A SIMILAR PROBLEM

I was facing a similar problem. I had a file named "myfile*" inside multiple subdirectories of my current directory. I wanted to edit a small change in all of them without getting to open them one by one. So, in the command line, I wrote this :

$find . -type f -name "myfile*" -exec vim -f {} \;

This way, find runs vim for each file. As soon as I quit an instance of vim, find automatically runs another with the next file until all of them have been done. Although, I agree that it would have been better if all the files could have been opened as tabs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文