Insert mode: inject predefined text via hotkey

I feel a bit silly for asking this, but I have not been able to figure out a solution.

I would like to insert-mode bind some key combination to some predefined string.
Eg. when I hit <C-m> in insert mode, I would like |> to be injected (the Elixir pipe operator, it is a pain to type it manually with my keyboard layout).

How do I do this in a Lua config file?

Are you using init.vim or init.lua?

You have to solve two issues:

1 - Escaping the pipe
2 - Use a different key binding because Ctrl-m is already in use, see `:h ctrl-m`

:inoremap <c-e> \|>

Another option:

:inoreab ,, \|>

In this case the abbreviation will expand when you hit space

Thank you, this helped me.

Turns out the issue was caused by <C-m> being occupied. The backslash was not required, in fact, it cause an error with my init.lua implementation:

local function map(mode, lhs, rhs, opts)
  local options = {noremap = true}
  if opts then options = vim.tbl_extend('force', options, opts) end
  vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end

map('i', '<C-e>', '|>')  # This works fine without '\'
1 Like

use vim.keymap.set with expr set to true and a Lua function on rhs that returns the text of your choice.