Speed up your Vim macros

Macros are one of the many productive features that draw users to Vim. If you've ever had to perform a repetitive set of functions across a large batch of data, you'll hopefully be familiar with concept of macros. If not, then I'd recommend reading Vi and Vim Macro Tutorial: How To Record and Play for an intro.

Do your Vim macros appear slow?

In its simplest form, a macro is a recorded set of keystrokes that can be saved and resent when required. In many cases, you will want to repeat the macro through multiple lines of code. However, Vim will update the screen at each step of the macro and this could cause a simple line edit to take a number of seconds to complete. Multiply this by the 10,000 lines that you hope to process and you could be in for some wait.

Enabling LazyRedraw

There is a simple way to instruct Vim not to update the screen until the macro has completed and, in most cases, this offers a great reduction in processing time. The command is LazyRedraw and is enabled either by running set lazyredraw within Vim or adding it to your .vimrc. Once enabled, you may run your macro across many thousands of lines and you'll see a significant improvement in speed. Remember, it may seem at first that your macro is not running, but give it time, the screen will only redraw once the macro has completed.