dein.vim icon indicating copy to clipboard operation
dein.vim copied to clipboard

[Discussion] equivalence of example .vimrc in Lua?

Open SichangHe opened this issue 3 years ago • 3 comments

This is not a bug report. I just want to discuss a bit about using dein in init.lua.

Problems summary

I could not find any instructions from the internet on how to use dein in init.lua. Though, I found people asking for Lua apis. I suppose posting my solution on dein's issue might help some people find theirs.

My solution

This is done by calling Vim functions from Lua.

In lua/dein.lua: (Edit: simplified. Edit2: Noice: NvimConfigPath and HomeDir are global variables defined in init.lua, see below. )

Dein = {}
local set = vim.opt
local fn = vim.fn
local add = fn['dein#add']
local begin = fn['dein#begin']
local check_install = fn['dein#check_install']
local install = fn['dein#install']
local end_ = fn['dein#end']

function Dein.activate()
    -- Neovim is always "nocompatible".

    -- Required:
    -- Add the dein installation directory into runtimepath
    local dein_path = NvimConfigPath .. "dein" -- Replace with yours.
    set.runtimepath:append(dein_path)

    -- Required:
    local cache_path = HomeDir .. "/.local/share/dein" -- Replace with yours.
    begin(cache_path)

    -- Let dein manage dein
    add(dein_path)

    -- Add or remove your plugins here like this:
    -- add('Shougo/neosnippet.vim')
    -- add('Shougo/neosnippet-snippets')

    -- Required:
    end_()

    -- Syntax highlighting is enabled by default on Neovim.

    -- If you want to install not installed plugins on startup.
    -- if check_install() == 1 then install() end
end

return Dein

And call this function in init.lua

-- `~/.config/nvim/`
NvimConfigPath = debug.getinfo(1).source:match("@?(.*/)")
-- `~`
HomeDir = os.getenv('HOME')

-- other stuff

require('dein')
Dein.activate()

Please critisize 🙏. Otherwise, feel free to use it.

SichangHe avatar Jul 31 '22 10:07 SichangHe

Sorry, I don't use Lua for the config. Other people can discuss for it.

Shougo avatar Jul 31 '22 11:07 Shougo

Thank you I've been looking for something like this I am new to vim and porting vimscript to lua has been a challenge. However I get this error when I run neovim :

Error detected while processing /home/niceiq/.config/nvim/init.lua: E5113: Error while calling lua chunk: Vim:E117: Unknown function: dein#begin stack traceback: [C]: in function 'begin' /home/niceiq/.config/nvim/lua/user/plugins.lua:21: in function 'activate_dein' /home/niceiq/.config/nvim/init.lua:6: in main chunk

I probably didn't set up some paths right so it didn't recognise the function. Here is what my plugins.lua file look like :

local set = vim.opt
local fn = vim.fn
local add = fn['dein#add']
local begin = fn['dein#begin']
local check_install = fn['dein#check_install']
local install = fn['dein#install']
local end_ = fn['dein#end']

-- 'path' would be init.lua's parent directory.
-- That is, '~/.config/nvim'.
function activate_dein(path)
    -- Neovim is always "nocompatible".

    -- Required:
    -- Add the dein installation directory into runtimepath
    local dein_path = string.format("home/niceiq/.cache/dein/repos/github.com/Shougo/dein.vim", path) -- Replace with yours.
    set.runtimepath:append(dein_path)

    -- Required:
    local cache_path = string.format("/home/niceiq/.cache/dein", path) -- Replace with yours.
    begin(cache_path)

    -- Let dein manage dein
    add(dein_path)

    -- Add or remove your plugins here like this:
    -- add('Shougo/neosnippet.vim')
    -- add('Shougo/neosnippet-snippets')

    -- Required:
    end_()

    -- Syntax highlighting is enabled by default on Neovim.

    -- If you want to install not installed plugins on startup.
    if check_install() == 1 then install() end
end

coeusiq avatar Aug 13 '22 15:08 coeusiq

    local dein_path = string.format("home/niceiq/.cache/dein/repos/github.com/Shougo/dein.vim", path) -- Replace with yours.

@coeusiq I think you are missing the leading forward slash for the string literal and you did not use the function correctly. string.format takes a string literal and replaces the %s and other place holders in it with the variables after the comma. The reason why I used it was because I wanted to concatenate path with /../../.local/share/dein and this was the solution I found. I have since changed the code and used .. to concatenate string and did other simplifications. What you want instead was probably something like:

    local dein_path = "/home/niceiq/.cache/dein/repos/github.com/Shougo/dein.vim"

However, this code will not work under another user's config, so I have a portable solution. I updated the example above. Please have a look and see if it would be clearer.

SichangHe avatar Aug 13 '22 22:08 SichangHe