Neovim Dev (1)

04 Dec 2022 . category: . Comments
#lua #neovim

Here is introduction of neovim lua plugin development. You can also check nvim-lua-guide for more configuration and development.

Introduction

Here is the basic lua plugin structure.

📂 ~/your_path_of_plugin
├── 📂 lua
│  └── 📂 plugin_name
│     └── 🌑 init.lua
├── 📁 plugin
│     └── 🌑 plugin_name.lua

In your plugin/plugin_name.lua,

require('plugin_name')

this will load your plugin.

To avoid identical name of plugins, we not use directly lua/init.lua. Using lua/plugin_name/init.lua will be a better choice.

Lists of directories for runtime files

📂 ~/your_path_of_plugin
├── 📂 lua
│  └── 📂 plugin_name
│     └── 🌑 init.lua
├── 📁 plugin
│     └── 🌑 plugin_name.lua
├── 📁 ftplugin/    filetype plugins write-filetype-plugin
├── 📁 indent/      indent scripts indent-expression
├── 📁 keymap/      key mapping files mbyte-keymap
├── 📁 lang/        menu translations :menutrans
├── 📁 lua/         Lua plugins
├── 📁 pack/        packages :packadd
├── 📁 parser/      treesitter syntax parsers
├── 📁 plugin/      plugin scripts write-plugin
├── 📁 print/       files for printing postscript-print-encoding
├── 📁 query/       treesitter queries
├── 📁 rplugin/     remote-plugin scripts
├── 📁 spell/       spell checking files spell
├── 📁 syntax/      syntax files mysyntaxfile
├── 📁 tutor/       tutorial files :Tutor
├── 📁 doc/         documentation write-local-help
├── 📁 compiler/    compiler files :compiler
├── 📁 colors/      color scheme files :colorscheme
├── 📁 autoload/    automatically loaded scripts autoload-functions
├── 🌑 filetype.lua filetypes new-filetype
├── 🇻 menu.vim     GUI menus menu.vim

More info about load-plugins: :help load-plugins

Create your plugin command

Here is the example min-preview.nvim I created for this blog.

📂 ~/min-preview.lua
├── 📂 lua
│  └── 📂 min-preview 
│     └── 🌑 init.lua
├── 📁 plugin
│     └── 🌑 min-preview.lua

We will use nvim_create_user_command({name}, {command}, {*opts})

nvim_create_user_command({name}, {command}, {*opts})
{command} is the replacement text or Lua function to execute.

In plugin/min-preview.lua, I create plugin command.

local api = vim.api
api.nvim_create_user_command('Previewmd', "lua require('min-preview').previewmd()" , {})
api.nvim_create_user_command('Previewjy', "lua require('min-preview').previewjy()" , {})

You can also use lua function.

local api = vim.api
api.nvim_create_user_command('Previewmd', function ()
  local preview = require('min-preview')
  preview:previewmd()
end, {})

Resources

Here are some useful resources for neovim lua plugin development



Me

John Smith is an awesome person. He lives in Flatland, where he works on two-dimensional engineering projects. In his spare time, John likes to eat cotton candy.