Can I use vim to open a huge file effectively?

sometime I need to open a several GB log file, now I need to use tail to get suitable part but that is very tedious.

Can I just use vim to open it directly? Currently vim will occupy a lot memory when open a large file... Just wonder why it is required? If there some option can prevent that memory loaded?


Solution 1:

If you just want to have a look at a file, use less.

If you need cut out a specific part of the file, do this (within less):

  1. Go to the 1st page you're interested in

  2. Press the keys m and a - this means: mark position a.

  3. Go to the last position of the file you're interested in

  4. Press the keys | (pipe) and a - this means: pipe everything between here and position a to a command

  5. Type in the command you want the data to be sent to. If you want to copy this data into the editor buffer, just use vim - as command (the minus sign as filename means file to be edited is to be read from stdin).

If you know that you will need all data from a specific position on up to the end of the file you may also do this:

  1. Go to the 1st page you're interested in

  2. Press the keys | (pipe) and $ - this means: pipe everything between here and the end of the file to a command

  3. Type in the command you want the data to be sent to (e.g. cat >/tmp/myfile or vim -).

Solution 2:

Check out this and this.

Basically, your options are:

  1. Give the Vim Large File script a try. It disables certain features to improve Vim's responsiveness when editing large files.
  2. Break the file up to smaller chunks, edit the chunk you care about and concatenate them back together.

Solution 3:

Yes, Vim can open very large files. But, in order to conserve memory some things you should probably keep in mind before doing so:

  • ignore filetype (no syntax highlighting and so on), although in all probability it won't have syntax highligting for log files at all, but just to be sure so it doesn't start going through them
  • noswapfile (you definitely don't want a copy of a file that size)
  • bufhidden = unload (save memory when other file is viewed)
  • buftype = nowritefile (is read only)
  • undolevels = -1 (no undo possible)

Maybe there is an option or two I missed, but these are so far in my _vimrc.