Search code examples
neovim

Map key for multiple combinations in neovim


I'm using a Swedish/Finnish keyboard layout and am trying to map the {,},[,] characters to a more convenient key.

I tried putting the following lines in my init.lua

vim.keymap.set({ "i", "o" }, "å", "[", { noremap = true })
vim.keymap.set({ "i", "o" }, "¨", "]", { noremap = true })
vim.keymap.set({ "i", "o" }, "Å", "{", { noremap = true })
vim.keymap.set({ "i", "o" }, "^", "}", { noremap = true })

which works well in insert mode but not in combination with other commands. For instance, when I press I want the cursor to move to { but what happens is that it moves to Å instead.

Any idea how to solve this without explicitly mapping every single combination?


Solution

  • This should work for both and ciå.

    for _, entry in ipairs({
        { { "l", "c" }, "å", "[" },
        { { "l", "c" }, "¨", "]" },
        { { "l", "c" }, "Å", "{" },
        { { "l", "c" }, "^", "}" },
        { { "o", "v" }, "iå", "i[" },
        { { "o", "v" }, "i¨", "i]" },
        { { "o", "v" }, "iÅ", "i{" },
        { { "o", "v" }, "i^", "i}" },
        { { "o", "v" }, "aå", "a[" },
        { { "o", "v" }, "a¨", "a]" },
        { { "o", "v" }, "aÅ", "a{" },
        { { "o", "v" }, "a^", "a}" },
    }) do
        local modes, from, to = unpack(entry)
        vim.keymap.set(modes, from, to, { noremap = true })
    end
    vim.opt.iminsert = 1