在 VIM 中处理大文件 [英] Working with huge files in VIM
问题描述
我尝试在 VIM 中打开一个巨大的 (~2GB) 文件,但它卡住了.我实际上不需要编辑文件,只需高效地跳转即可.
I tried opening a huge (~2GB) file in VIM but it choked. I don't actually need to edit the file, just jump around efficiently.
如何在 VIM 中处理非常大的文件?
How can I go about working with very large files in VIM?
推荐答案
我今天有一个 12GB 的文件要编辑.vim LargeFile 插件对我不起作用.它仍然用光了我所有的内存,然后打印了一条错误消息:-(.我不能使用 hexedit,因为它不能插入任何东西,只能覆盖.这是另一种方法:
I had a 12GB file to edit today. The vim LargeFile plugin did not work for me. It still used up all my memory and then printed an error message :-(. I could not use hexedit for either, as it cannot insert anything, just overwrite. Here is an alternative approach:
您拆分文件,编辑部分,然后重新组合.不过,您仍然需要两倍的磁盘空间.
You split the file, edit the parts and then recombine it. You still need twice the disk space though.
搜索您要编辑的行周围的内容:
Grep for something surrounding the line you would like to edit:
grep -n 'something' HUGEFILE | head -n 1
提取文件的那个范围.假设您要编辑的行位于第 4 行和第 5 行.然后执行:
Extract that range of the file. Say the lines you want to edit are at line 4 and 5. Then do:
sed -n -e '4,5p' -e '5q' HUGEFILE > SMALLPART
- 需要
-n
选项来抑制 sed 打印所有内容的默认行为 4,5p
打印第 4 行和第 5 行5q
在处理第 5 行后中止 sed- The
-n
option is required to suppress the default behaviour of sed to print everything 4,5p
prints lines 4 and 55q
aborts sed after processing line 5
使用您喜欢的编辑器编辑 SMALLPART
.
Edit SMALLPART
using your favourite editor.
合并文件:
(head -n 3 HUGEFILE; cat SMALLPART; sed -e '1,5d' HUGEFILE) > HUGEFILE.new
- 即:从 HUGEFILE 中选择编辑行之前的所有行(在本例中是前 3 行),将其与编辑行(在本例中为第 4 行和第 5 行)合并并使用此组合行集替换 HUGEFILE 中的等效项(在本例中为前 5 行)并将其全部写入新文件.
HUGEFILE.new
现在将成为您编辑的文件,您可以删除原来的 HUGEFILE
.
HUGEFILE.new
will now be your edited file, you can delete the original HUGEFILE
.
这篇关于在 VIM 中处理大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!