How does Vim opens big files so fast ? - vim

I opened a PDF file in vim which was about 4MB and it opened it at the blink of eye. I was amazed at the speed. So I tried the same in notepad and it crashed. I tried in notepad++, it took time.
Does anyone knows how vim handles this scenarios ? What makes reading files in vim so fast ?

Vim reads the whole file into an internal buffer on opening, so this all depends on your (virtual) memory and overall computer performance. Some disk editors only read in (viewed) parts of the file dynamically, and thereby obtain even greater performance.
On todays hardware, 4 MB is nothing. You can still drag down Vim with files of 100s of MB, especially if long lines or syntax highlighting is involved.
Other editors should not have a problem with 4 MB, neither. But PDF is not a text format, it's binary, so that might have confused some editors or taken them a long time to figure out what it's about.
The LargeFile plugin is worth mentioning here.

Related

Why notepad takes taking longer than LTViewr

Why Notepad takes 25 seconds to open a 6MB text file
While Large Text File Viewer takes 10 seconds to open 600MB text file?
Most likely, Notepad is loading (or is attempting) to load the entire file into RAM memory, whereas LTFV is simply caching a certain portion of the file, but not the full content. You will also notice that for very large files (say 100MB or more), Notepad will tend to have a herky jerky performance as you scroll through it, while tools such as LTFV or Notepad++ will have smooth performance even for very large files.

Recover unsaved changes in vim

I had unsaved changes to the file foo.txt in a vim buffer before my computer crashed. Running vim -r showed that there are no swap files in the directory containing foo.txt.
Is there any way to recover these changes?
Darn, okay. Is there a setting I could turn on to enable automatic writes
to a swap file on each change?
read :h swap, specially followings:
Updating the swapfile ~
The swap file is updated after typing 200 characters or when you have
not typed anything for four seconds. This only happens if the buffer
was changed, not when you only moved around. The reason why it is not
kept up to date all the time is that this would slow down normal work
too much. You can change the 200 character count with the
'updatecount' option. You can set the time with the 'updatetime'
option. The time is given in milliseconds. After writing to the swap
file Vim syncs the file to disk. This takes some time, especially on
busy Unix systems. If you don't want this you can set the 'swapsync'
option to an empty string. The risk of losing work becomes bigger
though. On some non-Unix systems (MS-DOS, Amiga) the swap file won't
be written at all.
If the writing to the swap file is not wanted, it can be switched off
by setting the 'updatecount' option to 0. The same is done when
starting Vim with the "-n" option. Writing can be switched back on by
setting the 'updatecount' option to non-zero. Swap files will be
created for all buffers when doing this. But when setting
'updatecount' to zero, the existing swap files will not be removed, it
will only affect files that will be opened after this.

Why is it slower to print directly to console/terminal than redirecting?

Why does it take significantly more time to print multiple lines to the terminal rather than redirecting it to a file which seems to be almost instant ?
Primarily, terminals are just plain slow. For every update, they have to:
Parse and interpret any control codes.
Parse, interpret and render any escape codes.
Interpret and account for any multibyte and composed characters.
Update appropriate screen and scroll buffers.
Render this with appropriate fonts.
and possibly do all of the above over again if you use screen or tmux.
Meanwhile, for redirecting to a file, you just have to:
Dump data into RAM (for later writeback to storage).
This step is so minor that it doesn't even register on the terminal's checklist.
This is not something people optimize for, because the speed of your terminal is rarely an issue. The difference between terminals can be 50x (VGA vs fbcon back in the day), and you can time it with a simple time cat somebigfile.txt.

Searching through really big files

I need to search through a TB of raw hard disk data. I need to find a couple of things inside. I tried using sudo cat /dev/sdc | less but this fails because it puts everything into RAM that is read. I only have 8 GB of RAM and 8 in swap space so putting a whole TB of data into RAM will not work.
I was wondering if I could somehow make less forgot what it has read after the 1GB mark or maybe use another editor.
I accidentally repartitioned my drive and lost some important files. I tried some utilities but none of them worked so I tried this. I got a few of the files but I can't get the rest because the computer freezes and runs out of RAM.
I learned my lesson, I need to make more frequent backups. Any help is greatly appreciated.
The -B option to less is exactly what you ask for. It allows less to be forgetful. Combine with -b1048576 to allocate 1G (the -b unit is K)
Or do it the interactive way: run less normally, scroll down until the point where it starts to get a little laggy, then just type -B at the less prompt to activate the option (did you know you can set less options interactively?)
Just don't try to scroll backward very far or you'll be forgotten-content land, where weird things happen.
(Side note: I've done this kind of recovery before, and it's easier if you can find the filesystem structures (inode blocks etc.) that point to the data, rather than searching for the data in a big dump. Even if some of the inodes are gone, by first recovering everything you can from the surviving inodes you narrow down the range of unknown blocks where the other files might be.)

What files can I safely delete from the Vim install directory?

I have an old 256mb usb drive that I'm using as a place to store text files. Scratch notes, links, todo lists, and a personal journal logfile.
I installed vim onto the drive in order to always have a decent text editor, even if I'm switching between computers, quickly editing something on someone else's machine, etc.
I'm not worried about hitting the drive's capacity any time soon: the vim install directory itself takes up more space than all the other text, and vim's pretty small.
But if I was to somehow fill the thing with 2 million or so characters of text, what could I safely delete from the vim install directory to clear up some space?
(Of course this isn't an urgent question, just a fun exercise in minimalism.)
If you want absolute minimalism, you can delete everything and just keep vim.exe (and any required DLLs). You can also reduce vim.exe's size from 1.6 MB to 0.8 MB by using UPX. If you want to reduce vim.exe's size further, compile it yourself removing any flags that you don't need.
You can additionally include your .vimrc, some syntax files, your favourite plugins, etc. The size of this depends on you.
So theoretically you could get your vim installation below 0.8 MB.
I'd start by removing syntax, filetype, and compiler plugins you never intend to use. All the colorschemes could also go, to be replaced with just the one you use (if any).
Assuming you are on Windows, you are going to notice that installation from vim.org uses around 30 mb, which leaves space for plenty text.
If you still believe it is necessary to have Vim use less space, you could try the portable version from PortableApps, which is very stable and uses ~15 mb.
Edit:
Actually the latest gVim Portable (7.4) doesn't save much space. The ~15 mb mentioned refers to version 7.3, which was compiled with a reduced set of features (e.g.: -perl/dyn).
Instead of deleting files as filetype plugins and colorschemes, which are usually very small, you should consider compile Vim enabling only the features you actually use. There are a large number of such features, as can be seem with :version command.

Resources