What is the usual way of disabling built-in commands, e.g., when building a modal dialog?

I am building a plugin and my plan is to create a modal interaction using a floating window that I create using nvim_open_win. My plan is to draw the contents of my dialog by setting the lines of a buffer that I create manually. This is working fine for me but let me know if there is a better way supported by the API.

My question is, I will want the user to interact with my dialog using a few dialog-specific key mappings. I can set those mappings and that works fine. But by default the user can also edit the text of my dialog using the built-in commands (dd, etc.). Is there a standard idiom for disabling/shadowing all of the built-in mappings in a custom dialog like this?

1 Like

Could you maybe make the buffer readonly?

If I just set ro in a random Neovim buffer, it doesn’t even stop me from entering Insert mode using the normal bindings and editing the buffer – I just get a warning that I’m editing a RO buffer. I also want to remove mappings like : and / and ?, etc., which set ro won’t do.

For example, I’m using this now, but this seems like overkill on the one hand, and on the other hand I haven’t actually unmapped everything (<Tab>, etc.):

local function map_to_nop(buf, keystroke)
  vim.api.nvim_buf_set_keymap(buf, 'n', keystroke, '', {nowait=true})
end

function shadow_all_global_mappings(buf)
  -- We only need to worry about normal mode, because with no mappings available it should not even be possible to get into another mode.
  chars = 'abcdefghijklmnopqrstuvwxyz'
       .. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
       .. '0123456789'
       .. '~!#$%^&*()_+`-=[]{}|\\:;"\'<>,.?/'

  for i = 1, #chars do
    char = chars:sub(i, i)
    print(char)
    map_to_nop(buf, char)
    map_to_nop(buf, string.format('<C-%s>', char))
    map_to_nop(buf, string.format('<A-%s>', char))
  end
end

vim.cmd [[mapclear]] check the documentation, I’m sure there’s some ARGS to spice it up

1 Like

Thanks for the pointer, but :mapclear doesn’t disable built-in commands like dd, it just unmaps a small handful of random mappings created by default on startup, see :help default-mappings.

@andmis it wouldn’t remove command-line mappings etc but you could :set nomodifiable which at least prevents a user from entering insert mode or otherwise modifying the buffer.