A community-driven vim distribution

Home | About | Quick start guide | Documentation | Development | Community | Sponsors

Blogs » Use Lua to Reimplement Core Plugins

After comparing the speed of vim script and lua, I want to rewrite SpaceVim with Lua.

NOTE: All vim scripts will be retained to ensure support for older versions of Vim and NeoVim

How to compare speed

with following vim script, and run:

function! Fibo(N) abort
    let t = a:N
    let b = 0
    while t > 0
        let t = t - 1
        let a = 1
        let b = 1
        let c = 73
        while c > 0
            let c = c - 1
            let tmp = a + b
            let a = b
            let b = tmp
    echo b

function! LuaFibo(N) abort
lua << EOF
local a, b, r, c, t
t = vim.api.nvim_eval("str2nr(a:N)")
while t > 0 do
        t = t - 1
        a = 1
        b = 1
        c = 73
        while c > 0 do
                c = c - 1
                r = a + b
                a = b
                b = r

print(string.format("%d", b))
function! s:test(...) abort
        " for func in a:000
                let start = reltime()
                call call(a:1,[a:2])
                let sec = reltimefloat(reltime(start))
                echom printf('%s(%d): %.6g sec', a:1, a:2, sec)
        " endfor

command! -nargs=+ TestFunc call s:test(<f-args>)


Fibo(1000): 0.410364 sec
Fibo(10000000): 1470.280914 sec
LuaFibo(1000): 9.052000e-4 sec
LuaFibo(10000000): 1.235385 sec

Which version of neovim should I use?

It is recommended to use neovim 0.9.0 or 0.9.1

What has been done?


The default runtime logger can be used in in both vim script and lua script. For example:

-- in lua script test.lua

local log = require('spacevim.logger').derive('test')

local function test_foo()
  log.debug('this is debug message')


Then the runtime log via SPC h L, and you should be able to see:

[     test ] [17:14:56:907] [ Debug ] this is debug message


Built-in Plugins:

These plugins are very commonly used in SpaceVim, so they are rewritten using lua for a better experience.

The next step

Please subscribe to the SpaceVim blog to get the latest updates.

Powered by Jekyll, Help improve this page