1

I am trying to write a pretty-printer Lua plugin for a LaTeX-like language. The idea is that if a user types \cup in insert mode then presses <Space> or <Tab> or <CR>, then my script will read the preceding string and transform it into . So far I have:

_G.rewrite_unicode = function(keystroke)
  local r, c = unpack(vim.api.nvim_win_get_cursor(0))
  return keystroke
end

vim.api.nvim_set_keymap('i', '<Space>', "v:lua.rewrite_unicode('<Space>')", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<Tab>', "v:lua.rewrite_unicode('<Tab>')", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<CR>', "v:lua.rewrite_unicode('<CR>')", { noremap = true, expr = true }

but I don't know how I can read the string preceding the cursor position in the buffer, then rewrite it. What APIs would I use for that?

1
  • 1
    You probably just want insert-mode abbreviations (:help inoreabbrev). Snippets if you want to go more powerful than that.
    – D. Ben Knoble
    Commented Sep 15, 2021 at 19:18

1 Answer 1

1

A far simpler version of what you're trying to accomplish:

inoreabrrev <buffer> \cup ∪

To read/write a buffer, you probably want the API at :help text-functions.

If you meant register, you can access it directly @a to read/write it.

If you need to do "smarter" things, I would look for a snippets plugin.

1
  • Thank you yes, I used abbreviations. Thanks for the pointers for future reference though.
    – ahelwer
    Commented Sep 22, 2021 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.