简体   繁体   English

在Vim中只打开文件的前几行

[英]Open only the first few lines of a file in Vim

I'm working with a CSV that's just shy of 1 GB. 我正在使用一个只有1 GB的CSV。 I want to see the the file's structure and a sample of the data, but I don't want to open the entire file. 我想查看文件的结构和数据样本,但我不想打开整个文件。 How can I load the first few rows in Vim? 如何在Vim中加载前几行? If it makes a difference, I'm using MacVim. 如果它有所作为,我正在使用MacVim。

我通常使用headtail类的命令来查看大文件的部分内容。

$head -10 <large file>

If you must do it from vim, use this: 如果您必须从vim执行此操作,请使用以下命令:

:r !head -10 path/to/big.file

That would get the first 10 lines of big.file and insert them into the current buffer. 这将获得big.file的前10行并将它们插入当前缓冲区。

If you are only wanting to preview and not edit the file, you can just use the less or more (see comment below) command. 如果您只想预览而不是编辑文件,则可以使用lessmore (请参阅下面的注释)命令。 You can use spacebar and enter to read more of the file, and q to exit. 您可以使用空格键并输入以读取更多文件,然后按q退出。

$ vim '+%!head -10' FILE

What that does is open the file, then execute :%!head -10 , which pipes the entire buffer through head -10 and replaces the contents of the buffer with the output from head . 这样做是打开文件,然后执行:%!head -10 ,它通过head -10整个缓冲区,并用head的输出替换缓冲区的内容。

Mind the quotes, by the way. 顺便提一下,请注意引号。

For anyone looking at how to do this on Windows, you can use the following in PowerShell to open the first 10 lines of a file in gvim : 对于在Windows上查看如何执行此操作的任何人,您可以在PowerShell中使用以下内容在gvim打开文件的前10行:

PS C:\MyCSVDirectory> Get-Content .\myfile.csv -TotalCount 10 > out | gvim out

Personally I prefer having it opened in gvim , but if you'd like to have it open in terminal just use vim instead. 我个人更喜欢在gvim打开它,但如果你想在终端打开它只需使用vim Also note that this will create a file called out in the directory of your csv. 另请注意,这将在csv的目录中创建一个名为out的文件。

Alternatively you can use the following to simply print the lines in terminal: 或者,您可以使用以下内容简单地在终端中打印行:

PS C:\MyCSVDirectory> Get-Content .\myfile.csv -TotalCount 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM