12 Mar 2021

Convert Neovim config from init.vim to init.lua

Only a few days months left until Neovim 0.5 stable release, with the support of init.lua as default user config. Here's a quick guide (more like a cheat sheet) to help someone with no Lua experience like me to save a few hours :moon:

Config path

~/.config/nvim/init.vim~/.config/nvim/init.lua

Comment

" init.vim
" This is a comment
-- init.lua
-- This is a comment

Options

" init.vim
set mouse=a
set tabstop=4
set number
set shortmess+=c
-- init.lua
vim.opt.mouse = 'a'
vim.opt.tabstop = 4
vim.opt.number = true
vim.opt.shortmess:append('c')

Global variables

Simmilar to the above

" init.vim
let g:mapleader = ' '
-- init.lua
vim.g.mapleader = ' '

For variables with namespace, Lua doesn't support # so you have to put them in ['']:

" init.vim
let g:sneak#label = 1
-- init.lua
vim.g['sneak#label'] = 1

Key mappings

" init.vim
nnoremap <C-s> :write<CR>
-- init.lua
--              <mode>  <keys>    <actions>       <options>
vim.keymap.set( 'n',   '<C-s>', ':write<CR>', {noremap = true})

Plugin manager

I've switched to paq-nvim, written in Lua, very similar to vim-plug.

September 2021 update: I've switched to packer.nvim for more features.

May 2023 update: I've switched to lazy.nvim for better lazy loading and performance (even the author of packer.nvim switched), you can view my lastest configuration here.

Install the plugin manager:

-- init.lua
-- Auto install if not exist
local install_path = fn.stdpath('data')..'/site/pack/paqs/opt/paq-nvim'

if fn.empty(fn.glob(install_path)) > 0 then
  cmd('!git clone --depth 1 https://github.com/savq/paq-nvim.git '..install_path)
end

-- Load the plugin manager
cmd 'packadd paq-nvim'

-- Set the short hand
local plug = require('paq-nvim').paq

-- Make paq manage it self
plug {'savq/paq-nvim', opt=true}

Install your plugins:

-- init.lua
plug 'joshdick/onedark.vim'
plug 'justinmk/vim-sneak'
plug 'neovim/nvim-lspconfig'

The commands is very similar too:

vim-plugpaq-nvim
:PlugInstall:PaqInstall
:PlugClean:PaqClean

Result

You can compare my old init.vim and the new init.lua.