Search code examples
neovim

How do I map space key for specific filetypes in nvim in insert mode?


I want to map double space (" ") to output ". " in insert mode, but only for md, tex and txt filetypes. I tried pretty much everything, making md.vim file in ftplugin directory, making an autocmd, but neither works.

The last autocmd I tried was

autocmd FileType md inoremap <buffer> <space><space> .<space>

It does not work. What do I do?

Tried pretty much everything I could think of using autocmd, just like in this issue.

Is there any solution? Preferably using lua, like vim.keymap.set() function


Solution

  • You had to set markdown as a filetype, your vimscript was correct, here is an example of how to do it in lua:

    local group = vim.api.nvim_create_augroup("markdown_autocommands", { clear = true })
    vim.api.nvim_create_autocmd("FileType", {
        pattern = {"markdown", "txt"}, -- here you can add additional filetypes
        callback = function(ev)
            -- actual mapping
            vim.api.nvim_buf_set_keymap(0, 'i', '<space><space>', '.<space>', { noremap = true, silent = true })
        end,
        group = group 
    })
    
    • Checkout neovim help for nvim_create_augroup_, and nvim_create_autocmd
    • Regarding clearance of autocommand groups and why bother creating groups at all.
    • I don't use tex files, so I don't know what name of this filetype in neovim, you can open a tex file and check its file type with :set filetype? command.