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时,我经常使用通配符搜索对我想编辑的文件很懒惰
vi ThisIsAReallLongFi*.txt
When this matches a single file it works great. However, if it matches multiple files vi does something weird.
如果匹配单个文件,则效果很好。但是,如果它匹配多个文件vi做了一些奇怪的事情。
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
第二,当我:wq退出文件时,我收到一条消息,终端的底部看起来像这样
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.
当我按下回车键时,它会返回我刚刚进入的文件中的编辑模式。我期望的行为是vi会转到下一个要编辑的文件。
So,
所以,
-
What's the logic behind vi's behavior here
vi行为背后的逻辑是什么?
-
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.
是的,我知道标签完成,这个问题是基于好奇心,并希望更好地理解shell。
2 个解决方案
#1
35
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.
vi支持有多个文件可供编辑。 :n转到下一个文件,:N转到上一个文件。使用:h arglist获取更多信息。
#2
7
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.
您可以编写:vim -p myfile *,vim会将当前目录中myfile *的所有匹配项打开到多个选项卡中。然后,您可以逐个编辑所有文件。使用gt导航到下一个选项卡,使用gT导航到上一个选项卡。要一次性保存和退出所有文件,只需在vim中写入:wqa。
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 :
我遇到了类似的问题。我在当前目录的多个子目录中有一个名为“myfile *”的文件。我想编辑所有这些中的一个小变化,而不是一个接一个地打开它们。所以,在命令行中,我写了这个:
$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.
这样,find为每个文件运行vim。一旦我退出vim实例,find会自动运行另一个文件,直到完成所有这些文件。虽然,我同意如果所有文件都可以作为选项卡打开会更好。
#1
35
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.
vi支持有多个文件可供编辑。 :n转到下一个文件,:N转到上一个文件。使用:h arglist获取更多信息。
#2
7
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.
您可以编写:vim -p myfile *,vim会将当前目录中myfile *的所有匹配项打开到多个选项卡中。然后,您可以逐个编辑所有文件。使用gt导航到下一个选项卡,使用gT导航到上一个选项卡。要一次性保存和退出所有文件,只需在vim中写入:wqa。
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 :
我遇到了类似的问题。我在当前目录的多个子目录中有一个名为“myfile *”的文件。我想编辑所有这些中的一个小变化,而不是一个接一个地打开它们。所以,在命令行中,我写了这个:
$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.
这样,find为每个文件运行vim。一旦我退出vim实例,find会自动运行另一个文件,直到完成所有这些文件。虽然,我同意如果所有文件都可以作为选项卡打开会更好。