简体   繁体   中英

Open only the first few lines of a file in Vim

I'm working with a CSV that's just shy of 1 GB. 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? If it makes a difference, I'm using MacVim.

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

$head -10 <large file>

If you must do it from vim, use this:

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

That would get the first 10 lines of big.file and insert them into the current buffer.

If you are only wanting to preview and not edit the file, you can just use the less or more (see comment below) command. You can use spacebar and enter to read more of the file, and q to exit.

$ 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 .

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 :

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. Also note that this will create a file called out in the directory of your csv.

Alternatively you can use the following to simply print the lines in terminal:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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