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.
-
http://www.vim.org/scripts/script.php?script_id=1506 – akira Mar 22 '11 at 10:39
5 Answers
I generally use a command like head
or tail
for seeing partial content of large files.
$head -10 <large file>

- 6,661
- 7
- 48
- 63

- 371
- 2
- 9
-
I'll use `head` and/or `tail` to limit the lines, then pipe to view using something like `head -1000 FILE | tail -100 | view -` to grab lines 900-1000 of a file. We occasionally get log files over 1GB so paring them down before opening them is important. – the Tin Man Mar 27 '11 at 10:01
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.

- 5,163
- 31
- 39
$ 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.

- 27,620
- 12
- 60
- 72
-
7If vim opens a 1GB file, isn't it going to take a long time before it has the buffer open so it can pipe it through head? It seems like it would be a whole lot faster to use `head -10 FILE | vim -` or do what El Isra suggested. – the Tin Man Mar 27 '11 at 09:56
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.

- 6,208
- 3
- 49
- 59
-
2Originally there was an app called `more`, which is replaced on a lot of systems by [`less`](http://www.greenwoodsoftware.com/less/faq.html#what) now. On Mac OS, `more` actually points to `less`. They're similar, only `less` has better navigation, and can scroll backwards, among other things. – the Tin Man Mar 27 '11 at 09:51
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

- 1,158
- 3
- 16
- 31