home ~ projects ~ socials

Create A Local Neovim Lua Plugin

Introduction

I'm starting to experiment more with Neovim plugins. Most of the directions/tutorials I found about how to set one up locally confuse me. This is what I came up with after a bunch of searching and trial and error. It uses the lazy1 plugin manager for installation.

The Steps

  1. Create the directory structure for a new_plugin.nvim plugin inside the a ~/workshop directory:

    mkdir -p ~/workshop/new_plugin.nvim/lua/new_plugin
  2. Create a .lua file at:

    ~/workshop/new_plugin.nvim/lua/new_plugin/init.lua

    With the contents:

    local M = {}
    
    function M.setup()
        vim.keymap.set("n", "<Leader>8", function() 
          M.do_something()
        end)
    end
    
    function M.do_something()
        print("Doing Something")
    end
    
    return M
  3. Find the require('lazy').setup({}) section inside ~/.config/nvim/init.lua and add

    require('lazy').setup({
        -- all the stuff
    })

    And add put this dir statement inside it like this:

    require('lazy').setup({
        { dir = "~/workshop/new_plugin.nvim" },
        -- all the other stuff
    })
  4. Somewhere after the require('lazy').setup({}) stuff add this line in the ~/config.nvim/init.lua file

    (NOTE: This is only necessary if you need to do specific setup stuff. The example shows setting a hotkey, but that can be done (and I like it better) in a keymapping file)

    require('new_plugin').setup()

This particular example prints Doing Something when you hit the leader key followed by 8. Nothing spectacular there. Just a simple function to show the plugin is working.

-- end of line --

References

This is the page where things finally clicked. It's got some extra cruft but it's what got me started.

Footnotes