Skip to content

Instantly share code, notes, and snippets.

@gaoDean
Last active August 16, 2022 08:29
Show Gist options
  • Save gaoDean/288d01dfe64da66569fb6615c767e081 to your computer and use it in GitHub Desktop.
Save gaoDean/288d01dfe64da66569fb6615c767e081 to your computer and use it in GitHub Desktop.
Minimal automatic list continuing for ordered and unordered lists for nvim in Lua
M.autolist = function()
local preceding_line = vim.fn.getline(vim.fn.line(".") - 1)
if preceding_line:match("^%s*%d+%.%s.") then
local list_index = preceding_line:match("%d+")
print(list_index .. "t")
vim.fn.setline(".", preceding_line:match("^%s*") .. list_index + 1 .. ". ")
vim.cmd([[execute "normal \<esc>A\<space>"]])
elseif preceding_line:match("^%s*%d+%.%s$") then
vim.fn.setline(vim.fn.line(".") - 1, "")
elseif preceding_line:match("^%s*[-+*]") and #preceding_line:match("[-+*].*") == 1 then
vim.fn.setline(vim.fn.line(".") - 1, "")
vim.fn.setline(".", "")
end
end
-- all of the above should go in your func file (for me its func.lua and ive just got a local M = {} and a return M at the end)
function map(mode, keys, output)
vim.api.nvim_set_keymap(mode, keys, output, { noremap = true, silent = true})
end
function imap(keys, output)
map("i", keys, output)
end
function au(evt, pat, cmd) -- (string|table), (string|table), (string)
vim.api.nvim_create_autocmd(evt, { pattern = pat, command = cmd, })
end
imap("<cr>", [[<cr><cmd>lua require('func').autolist()<cr>]])
au("FileType", "markdown", "setl comments=b:*,b:-,b:+,n:>")
au("FileType", "markdown", "setl formatoptions+=r")

Autolist

Minimal automatic list continuing for ordered and unordered lists for nvim in lua

Made a plugin for this

https://www.reddit.com/r/neovim/comments/wpoq51/autolistnvim_lua_powered_list_continuation_plugin/?utm_source=share&utm_medium=web2x&context=3

(cant be bothered to search for the link, this was in my clipboard so yeah)

supports:

  • tabs
  • autodeletes empty list bullets
  • indented lists with and

doesn't support:

  • renumbering of lists

Installation

If your lua config is organised, you should have a file for options, for mappings etc. Put the top function in some function file, and put everything below the "all of the above" comment into a mappings and autocommands file. You don't have to 'require' the file with the functions in the init, just "require('func').autolist()".

@gaoDean
Copy link
Author

gaoDean commented Aug 15, 2022

@gaoDean
Copy link
Author

gaoDean commented Aug 16, 2022

Made a plugin for this, here it is: https://www.reddit.com/r/neovim/comments/wpoq51/autolistnvim_lua_powered_list_continuation_plugin/?utm_source=share&utm_medium=web2x&context=3

(cant be bothered to search for the link, this was in my clipboard so yeah)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment