neovimremap

Neovim keymap won't take effect until I manually source the file


Here is my config:

https://github.com/big-charter/nvim

Here is the keymap:

vim.keymap.set("i", "<C-s>", "<esc>yiwi<lt><esc>ea></><esc>hpF>a", { noremap = true, buffer = true })

If you type any word in insert mode, then <C-s>, it surrounds it like an html tag. Example - I type 'foo', remain in insert mode, press <C-s>, I'm left with '<foo></foo>' with my cursor in the middle of the tags.

This keymap is set in my lua/bigcharter/remap.lua file. In my top level init.lua, I require("bigcharter"), and in the init.lua file in my bigcharter dir, I have:

require("bigcharter.remap")
require("bigcharter.set")
require("bigcharter.packer")
require("bigcharter.format")

The remap does not work on startup. If I source the remap.lua file, the remap works. I only set my leader once, at the top of the remap file, and the remap does not even use the leader. When I use :map right after startup, I don't see it. When I use :map after sourcing remap.lua, I still don't see it, but it works. I have <C-s> remapped for normal mode in my harpoon.lua, however I tried remapping my keymap to something that's not mapped at all in any mode, <C-v>, and it still didn't work. I've added print statements before and after the remapping and I see them printed out. I see all of my other remappings in remap.lua when I use :map.


Solution

  • I have a felling that problem is here:

    .. buffer = true })
    

    in keymap.set - buffer stands for: (from :h keymap)

                 • "buffer" number|boolean Creates buffer-local mapping,`0`
                   or `true` for current buffer.
    

    So during startup you create this mapping only for a current buffer. Try removing buffer option.