Update your vimrc to Vim9 script

Vim 9 has been released, and one of the new features is Vim9 script. The purpose of Vim9 script, beside improve performance, is making Vimscript feels less weird, compare to other commonly used programming languages. In this blog post, we will learn how to convert existing vimrc file to use Vim9 script.

First step: vim9script

The vim9script command tells vim that your script is using Vim9 script instead of the "legacy" Vimscript. So the very first step is putting vim9script to the first line of your vimrc file.

Comments

The comment character changed from " to #, same as in Python and Ruby. For example:

" Delete comment character when joining commented lines
set formatoptions+=j

Should be converted to

# Delete comment character when joining commented lines
set formatoptions+=j

Variable declarations and assignments

Vim9script does not us let to declare a variable anymore. The new way to declare a variable is using var. For example:

let g:go_fmt_autosave=0
let a = 1
let a += 1

Should be converted to

g:go_fmt_autosave = 0
var a = 1
a += 1

Another thing to notice the space characters around = are now required.

Functions

Vim9 script is still allow using func/function to define a function, but the preferred new way is using def. For example

func Greeting(name)
  echo a:name
  return a:name
endfunc
call Greeting("Diep")

Should be converted to

def Greeting(name: string): string
  echo name
  return name
enddef
Greeting("Diep")

The changes are:

  • add types to function arguments and return
  • a: prefix should be removed
  • function call is now done without call

Another example, if you are using vim-plug:

call plug#begin()
Plug 'SirVer/ultisnips'
call plug#end()

It is now should be simplified to just

plug#begin()
Plug 'SirVer/ultisnips'
plug#end()

Line continuation and dictionary

First, the annoying line continuations can be removed, not just in dictionary but almost everywhere else.

Second, Vim9script is now using a JavaScript-like syntax for dictionary literal.

For example:

let g:lightline = {
      \ 'colorscheme': 'solarized',
      \ }

Should be converted to

g:lightline = {
  colorscheme: 'solarized',
}

Notice that quoting the key of dictionary is unnecessary now.

Conclusion

Q: Is Vim9 script faster?

A: Well, it might be faster for plugin developers, but for vimrc, there should be no noticeable different in performance.

Q: Is it worth converting all my vim scripts to Vim9 script?

A: IMO, no. The syntax looks a little better to read and write, but not by a huge margin. But if you have some free time, why not?