This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

How To Create An UI Menu In Neovim


One of many nice issues about neovim is the flexibility to easilty create plugins utilizing lua, lua is a quite simple language and the way in which that it is built-in with neovim is absolutely straight foward, when neovim is initiated it reads a file referred to as init.lua in one of many paths configured, you’ll be able to verify the trail by opening neovim and typing :h rtp for run time path you must see one thing like this
Image description

the primary path is the one which issues for us, it implies that neovim will seek for the file inside ~/nvim we will then go forward the create a file there or use the file that you have already got to create a perform, simply to check issues

we will create a perform to open a pop up menu utilizing plenary.popup like this, it’s good to set up neovim plenary when you do not have already got it https://github.com/nvim-lua/plenary.nvim

native popup = require("plenary.popup")

native Win_id

perform ShowMenu(opts, cb)
  native peak = 20
  native width = 30
  native borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }

  Win_id = popup.create(opts, {
        title = "MyProjects",
        spotlight = "MyProjectWindow",
        line = math.flooring(((vim.o.traces - peak) / 2) - 1),
        col = math.flooring((vim.o.columns - width) / 2),
        minwidth = width,
        minheight = peak,
        borderchars = borderchars,
        callback = cb,
  })
  native bufnr = vim.api.nvim_win_get_buf(Win_id)
  vim.api.nvim_buf_set_keymap(bufnr, "n", "q", "<cmd>lua CloseMenu()<CR>", { silent=false })
finish
Enter fullscreen mode

Exit fullscreen mode

the code is fairly self explanatory the one half that may be a little complicated is the cb parameter, this parameter is the perform that you simply gonna name when somebody intereact with the popup menu, additionally we create a keymap for the popup buffer to shut the popup once you hit q

vim.api.nvim_buf_set_keymap(bufnr, "n", "q", "<cmd>lua CloseMenu()<CR>", { silent=false })
Enter fullscreen mode

Exit fullscreen mode

this half may also be just a little complicated.
and the next is the code to shut the UI

perform CloseMenu()
  vim.api.nvim_win_close(Win_id, true)
finish
Enter fullscreen mode

Exit fullscreen mode

that is the perform that we gonna name after we hit q when the pop up menu is openened

Now let’s create one other perform to name the perform that reveals the menu

perform MyMenu() 
  native opts = {}
  native cb = perform(_, sel)
    print("it really works")
  finish
  ShowMenu(opts, cb)
finish
Enter fullscreen mode

Exit fullscreen mode

We not gonna do something right here for now simply open the menu with nothing inside after which you’ll be able to shut the menu by hitting q

after scripting this knowledge to ~/nvim/init.lua you’ll be able to load the file by working :so then you’ll be able to run :lua MyMenu() to name the perform and present the menu, you must see one thing like this

Image description

there’s cool however not helpful, how will we add knowledge contained in the menu? we will add knowledge there the identical manner that we do in any buffer, simply by setting traces of textual content, the plugin that we gonna construct is only a listing of tasks that we will selected from and the cd in to the challenge from vim.

we will add the traces of textual content by filling out opts the variable that we move to the menu like this

perform MyMenu()
  native opts = {
    "/house/me/myproject1",
    "/house/me/myproject2",
    "/house/me/myproject3",
  }
........
Enter fullscreen mode

Exit fullscreen mode

that is only a listing of issues to indicate within the menu when you do this you must be capable to see an inventory of these issues now

Image description

Superior!

We’re nearly there, now we simply have to do one thing when somebody choses one of many factor within the listing, we will do this within the cb that we move to the perform.

native cb = perform(_, sel)
  vim.cmd("cd " .. sel)
finish
Enter fullscreen mode

Exit fullscreen mode

when you hit enter on high of one of many traces, that line shall be handed to this perform after which we’ll change neovim present listing to that path, in our instance the paths aren’t actual however you get the thought, you simply have to put an actual path there, and that ought to do it, one improve that we will do is to load a file with paths as a substitute of hardcoding the paths within the perform, that manner you need not open this lua file everytime you wish to add a challenge to your listing of tasks, we will do this simply by studying a easy file with an inventory of tasks, similar an inventory of challenge in ~/tasks

/house/me/myproject1
/house/me/myproject2
/house/me/myproject3
Enter fullscreen mode

Exit fullscreen mode

now we will learn this file and add to the choices like this

native file = io.open("~/tasks", "r") -- Open the file in learn mode
native opts = {}
if file then
  for line in file:traces() do
    desk.insert(opts, line)
  finish
finish
--- move the opts to the file later
Enter fullscreen mode

Exit fullscreen mode

the very last thing we have to do is map this perform to a shortcut that manner we will hit the quick minimize and present the menu the place we will selected our challenge

vim.keymap.set("n", "<chief>o", '<cmd>lua MyMenu()<CR>')
Enter fullscreen mode

Exit fullscreen mode

and that is just about it, after you may have a manner of making a menu you’ll be able to factor about a number of interesint issues todo the thought introduced right here is simply to indicate a straightforward manner of making a menu in neovim, you need to use lua to do something that you really want

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?