Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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

share|improve this answer
1  
I did not know less could do this. Thanks :) –  icedwater Sep 5 '13 at 5:01

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.
share|improve this answer

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.

share|improve this answer
    
Note that (1) there's a plugin (vim.org/scripts/script.php?script_id=1506) for turning on several of these options automatically when a large file is opened, and (2) even with this program, opening files that are more than a couple GB in size can still be problematic, so I recommend following the "Ctrl-C" tip here: stackoverflow.com/a/19795855/1858225 –  Kyle Strand Oct 29 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.