Hi
I’m trying to get into lua/neovim and have an actual use case that I’d imagine would make a good entry point.
I’d like to write a shopping list, prefix the items with a tag (“d” for dairy etc.) and then have it reformatted (to markdown) and ordered by grocery shop sections.
I’d like to write a function that I can bind to a key that does some formatting and sorting to a selection:
Input
m chicken
p tomatoes
p cucumbers
d milk
d mozzerella
m ham
desired output (in place replacement of visual selection)
## meat
- [ ] chicken
- [ ] ham
## produce
- [ ] tomatoes
- [ ] cucumbers
## dairy
- [ ] milk
- [ ] mozzerella
Now I do have programming knowledge alongside some basic lua knowledge and writing the sorting and formatting part would be no big deal. But I have no idea about vim/lua scripting for (neo)vim.
I took a look at Getting started using Lua in Neovim but this seems to be more about using lua in neovim.
I have some general questions:
- I’m assuming this can be achieved via a function written in your init.lua config file or outside it and then import/require it (so no need to write a plugin), right?
- How would I be able to pass the visual selection to the function or retrieve it inside the function? I searched some docs and found stuff like
nvim_win_get_cursor({window})
but couldn’t find information about how to get visual selection. - How would I go about replacing the visual selection with the reformatted code?
Any pointers on how to do this or where to find resources about it are greatly appreciated.
Cheers!