How To Build a Simple Neovim Plugin

Adam Drake
4 min readJul 22, 2024

--

Curiosity killed the cat but what about the mouse? Made by generative AI.

In the Software world a language or tool will thrive or die depending on the strength of its ecosystem. You can have a great programming language but if no one is using it and putting time and effort into the community, then it will not survive very long.

I think this is very important when it comes to Vim and more specifically — Neovim. It is getting quite popular these days and I think one of the reasons for that is the community and the sheer amount of great plugins now available in the ecosystem.

You can have a great programming language but if no one is using it and putting time and effort into the community, then it will not survive very long.

I’ve used a bunch of plugins during my Neovim journey and gained a massive amount of benefits from those. Therefore, I wanted to take the time to learn how to make a simple plugin in Neovim and then share that experience here.

Getting Started

Just for ease of implementation I am going to be building this plugin in a standalone directory. A real life Neovim plugin would have its own git repository so it could easily be used by many people. I will not be doing that as the point of this article is to show the main pieces needed to build your own Neovim plugin.

Also you have to chose the language that you will be using to build this plugin. You can use Lua, Vimscript or some others like Python. I will be using Lua as it seems to be the language of choice when it comes to building Neovim plugins. This page is a great resource if you want to dive deeper into using Lua in Neovim.

Creating a init.lua File

As I said above I will be creating this plugin in a standalone folder for ease of development. Therefore I create this init.lua file here:

-- /myplugin/init.lua

For this plugin I’m going to make a simple “Hello world” print out when pressing a keymap. I appreciate that this isn’t particularly impressive but if you know how to do this then you just have to expand the function/s being called and learn about the Vim apis and you will be able to make some very impressive and useful plugins. This would be the code:

local M = {}

function M.hello_world()
print("Hello, World!")
end

-- Map a command to the function
vim.api.nvim_command('command! HelloWorld lua require("myplugin").hello_world()')

return M

Let’s break this down a bit:

  • the M variable is a common convention used to represent a module. This module can then act as a container for all your functions and variables that will be used by your plugin. It’s just a good way to organise your code.
  • Then we have a function that I hope is pretty self explanatory.
  • Then we use a method from the vim.api which in this case is nvim_command which allows you to execute the Neovim command :HelloWorld which will then run the hello_world function.
  • Returning the M at the end makes the content of this module available to other Lua scripts.

Running Your Plugin

To get your plugin to work you first have to either restart Neovim or run :source on the file. Once you have done that then you can run :HelloWorld and this should then print out ‘Hello, World!’. However you would have to do this everytime you make a change which would get pretty boring pretty fast. Therefore I will add a keymap to automate this step. To add a keymap you can do the following:

:nmap <Leader>my :write<CR>:source<CR>

So now, when pressing this keymap the code is saved and sourced and the plugin runs. Well done! You have successfully made your first Neovim plugin.

Conclusion

I tried in this article to keep things really simple to focus on the main things needed to build and get a plugin to run in Neovim. In the next couple of articles I will be expanding on this one and building out some more fancy and useful plugins. I am currently learning myself so I am sure there will be many things that I am overlooking or getting wrong but then that is all part of the journey.

I hope you have enjoyed this article and if you have any tips or pluging you have made yourself then please share. I would love to see them.

Subscribe to My Weekly Updates!

Enjoyed This Post?

If you found this blog post helpful, why not stay updated with my latest content? Subscribe to receive email notifications every time I publish. New posts go live every Monday at 8:30 AM Central European Time.

What You’ll Get

By subscribing, you’ll get:

  • Exciting Discoveries: Be the first to know about the latest tools and libraries.
  • How-To Guides: Step-by-step articles to enhance your development skills.
  • Opinion Pieces: Thought-provoking insights into the world of frontend development.

Join Our Community

I live in the vibrant city of Prague, Czech Republic, with my family. My blog is more than just articles; it’s a community of like-minded developers who share a love for innovation and learning.

About me

I’m a passionate Frontend Developer specialising in React and TypeScript. My professional journey revolves around exploring and mastering new tools and libraries within the JavaScript ecosystem.

Check out the original blog post on my blog.

Check out my LinkedIn and Github if you are interested.

--

--

Adam Drake
Adam Drake

Written by Adam Drake

I'm a Frontend Developer and I write about all things Frontend Related - Think lightly of yourself and deeply of the world. Based in Prague, CZ

No responses yet