Faster multi-file keyword completion in Vim? - vim

While searching for my python completion nirvana in vim, I have come to really love <C-x> <C-i>: "keywords in the current and included files". This almost always gets me a long nasty name from another module completed, which is great.
(Omni-completion is obviously better when it works, but too often it reports it can't find any matches. Ok, Python isn't Java, I get it)
The only problem with this multi-file completion is it's very slow: on my netbook, a file with a reasonable set of imports can take up to 4 or 5 seconds to parse every time I hit <C-x> <C-i>. It seems to load every imported file every time I hit <C-x> <C-i>. Is there any way to cache the files or speed up this process? Would using tag completion be faster?

It is quite possible that this process takes some time if you're working on projects with multiple source files (vim needs to parse all included source files to find more included source files and to build the word list.) You could use tag-completion, which uses the output of ctags to do almost the same, but you'd need to run a few tests to tell the speed difference.
I personally use complete completion (<C-P> or <C-N> in insert mode.) By default, it matches all words in all buffers (even buffers that have been unloaded, i.e. files that have been closed), but is really fast. I found that the completion works quite accurately nonetheless, even if you activate it after 2-3 characters.

Related

Testing buffer list with Vader

I am writing a vim plugin and I noticed a bug where temporary buffers are not cleaned up automatically. I am using the Vader test framework plugin to test it and I would like to be able to verify the fix with a Vader test and prevent regressions.
I would use :ls command to verify the buffer list doesn't grow but I don't know how to access the output of this from Vader.
So, is it possible to use Vader to test that the buffer list doesn't grow? Essentially, I just need to verify that the buffer list that is displayed is the same after executing my command as before. I don't need fine-grained check on the buffer list.
This isn't specific to Vader, but you can capture the output of any Ex command via :redir, or more comfortably (in newer Vim versions) with :help execute().
However, for the use case as you describe it, you don't need to parse the output of :ls at all: Buffers are allocated with increasing numbers, and the highest one can be obtained via bufnr('$'). So, just by storing the original value and comparing it with a later query, you can ensure that no additional buffers are created.
If you do need more more fine-grained checks on the buffers (your question isn't very clear on that), you can use filter() over all possible buffers, e.g. to obtain all listed buffers (or loaded buffers via bufloaded()):
:echo filter(range(1, bufnr('$')), 'buflisted(v:val)')

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.

Is there a way to debug cscope?

I'm trying to build a cscope code database (running cscope -Rb) on a reasonably-sized (not huge) codebase.
However, cscope is not returning. htop reports that cscope is using 100% of one CPU.
Over the past few months that I've been using it, cscope has completed creating the database (with previous iterations of the same codebase) in maybe as long as a minute.
I assume there's a bad code file somewhere that is causing the problem, but how do I determine which one it is if cscope just puts itself into what seems to be an infinite loop?

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