Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] equivalence of example .vimrc in Lua? #456

Open
SichangHe opened this issue Jul 31, 2022 · 3 comments
Open

[Discussion] equivalence of example .vimrc in Lua? #456

SichangHe opened this issue Jul 31, 2022 · 3 comments

Comments

@SichangHe
Copy link

SichangHe commented Jul 31, 2022

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.

@Shougo
Copy link
Owner

Shougo commented Jul 31, 2022

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

@coeusiq
Copy link

coeusiq commented Aug 13, 2022

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

@SichangHe
Copy link
Author

SichangHe commented Aug 13, 2022

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants