20

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.

Joe Mornin
  • 8,766
  • 18
  • 57
  • 82

5 Answers5

25

I generally use a command like head or tail for seeing partial content of large files.

$head -10 <large file>
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Vijay
  • 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
17

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.

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

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • 7
    If 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
2

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.

undefined
  • 6,208
  • 3
  • 49
  • 59
  • 2
    Originally 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
0

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
abcalphabet
  • 1,158
  • 3
  • 16
  • 31