Search code examples
vim

How do I unmap a key binding only in buffers of a filetype?


I have a mapping ;; to move to the end of line and insert a colon (useful in c-like languages). And in my scheme.vim file I have:

iunmap ;;

Problem with this is:

  1. It will try to run this command every time I open a scheme file, at which point it's already unmapped, so I will get a warning.

  2. It will unmap this key globally, so if I open a non-scheme file in the same session, it will not remember this binding.

At the very least I would like to solve #1, and have it only attempt to unmap if the binding is active so that I don't get an error.


Solution

  • It would probably be best to make your ;; mapping a buffer local mapping based on the 'filetype'.

    autocmd FileType c,javascript inoremap <buffer> ;; <esc>m'A;<esc>``a
    

    You can add as many filetypes as you need to this mapping. Just separate filetypes with commas.

    For more help see:

    :h :au
    :h :map-local
    :h 'filetype'
    :h FileType