在 vim 中编辑许多相同的文件并用一个命令将它们全部保存
由于未公开的原因,假设我在几十个子目录中有许多相同的 README.txt 文件。在我的幻想世界中,我可以运行这个:
vim --magic-mindreading-flag */README.txt
vim 不会按顺序为我编辑文件,而是会以某种方式识别它们是相同的,并同时保存我对所有文件的更改,并神奇地知道我要做什么。想。
请饶恕我以下内容:
- 如何使用 shell 命令执行此操作。
- 关于引用单个权威副本(例如使用符号链接)的讲座
- 建议使用您知道支持它的编辑器。
这是关于 vim 的,多么棒。
奖励:如果文件不相同,vim 会发出警告。
For reasons undisclosed, suppose I have many identical README.txt files in a few dozen sub-directories. In my fantasy world, I could run this:
vim --magic-mindreading-flag */README.txt
and rather than editing the files for me in sequence, vim will somehow recognize that they're identical and save my changes to all files simultaneously and magically know what I want.
Please spare me the following:
- How to do this with shell commands.
- Lecture on referencing a single authoritative copy (such as with symlinks)
- A recommendation to use an editor that you know of which supports it.
This is about vim, and how awesome.
Bonus: get vim will to warn if the files aren't identical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它们当前相同,您想要以相同的方式修改它们,并且它们要保持相同,我会这样做:
编辑第一个,然后删除所有其他的。然后我将所有其他符号链接到第一个。
If they are currently identical, you want to modify them identically, and they are to remain identical, I would do this:
Edit the first one, and then delete all the others. Then I would symlink all the others to the first one.
我将使用 vimdiff 同时编辑所有自述文件(向您显示任何差异;您可以使用 CTRL-W | 最大化当前窗口)。
然后要保存所有文件,请使用类似
:SaveAll
的命令(仅保存您的参数,而不保存您可能打开的任何其他缓冲区)。I would use
vimdiff
to edit all the README files simultaneously (showing you any differences; you can maximize the current window using CTRL-W |).Then to save all the files, use a command like
:SaveAll
(only saves your arguments, not any other buffers you may have opened).您可以在命令行上使用 vim。例如,将所有“旧”替换为“新”(但仅适用于 1 个文件)。
查找 vim 文档,看看是否可以对多个文件(
:bn
等)执行此操作。正如评论之一所建议的,vim 是一个编辑器,与 sed/ed/awk 相同。它的目的是编辑文件,因此使用 *nix 工具也是一样的。您可能需要考虑编写一个脚本来执行此操作。you can use vim on the command line. eg replace all "old" with "new" (but only for 1 file).
look up the vim docs to see if you can do it for mulitple files(
:bn
etc). As one of comments suggested, vim is an editor, same as sed/ed/awk. Its purpose is to edit files, therefore using *nix tools is just the same. you might want to consider writing a script to do that.好吧,您可以将更改记录到第一个文件中。我不知道通过更改缓冲区是否会持续录制,我怀疑并假设不会。如果只有几十个文件,我只需手动播放所有文件上录制的序列。如果您想要一个长期的、较少临时的解决方案,我希望可以创建一个在缓冲区之间循环的 vim 函数,并对每个缓冲区执行
normal @a
(假设您已经记录了您的更改寄存器 a) 中的第一个缓冲区,然后执行:wq
并在下一个缓冲区上重复。在 vim 中,执行
:help 10.1
和:help complex-repeat
。我认识到这不能满足您的要求“vim 不会按顺序为我编辑文件,而是会以某种方式识别它们是相同的,并同时保存我对所有文件的更改。”
Well, you could record your changes to the first file. I don't know if recording will persist through a change of buffer, I suspect and let's assume not. If it's only a few dozen files I would just manually playback the recorded sequence on all of them. If you want a longer-term, less ad-hoc solution, I expect it's possible to create a vim function that cycles between buffers and does
normal @a
on each of them (supposing you've recorded your changes to the first buffer in register a), then does:wq
and repeats on the next buffer.In vim, do
:help 10.1
and:help complex-repeat
.I recognize that this doesn't satisfy your request "and rather than editing the files for me in sequence, vim will somehow recognize that they're identical and save my changes to all files simultaneously."