4

I've written a Vim script that inserts Unicode mathematical notation into what I'm editing. It's basically a few hundred lines of code that look like :map! \bbq ℚ.

Sometimes I want to enable all of these definitions together. And sometimes I want to switch them all back off again. Really I want some kind of mode where they are enabled. But I'd like to switch them on and off while editing other types of file, like C++ source or plain text. What's the best way to organize this? Do I need to write a few hundred unmaps? Or is there some way of undoing a whole lot of mappings in bulk? Can I wrap this up in some kind of mode?

1 Answer 1

3

If you need those mappings only for certain kinds of files, define them in a script separate from your ~/.vimrc and as buffer-local mappings:

:map! <buffer> \bbq ℚ

You can then :source path/to/script.vim on demand. Alternatively, by putting the script into ~/.vim/ftplugin/{filetype}_mappings.vim, they will be automatically loaded whenever Vim's filetype detection (cp. :help filetype) recognizes {filetype} (e.g. tex for Latex documents).

If you still need to remove the mappings for the particular buffer, there's no way around defining corresponding :unmap <buffer> commands, or maybe using the hack of :set paste (which disables all mappings and has other side effects).

2
  • 2
    Could also use an expression mapping with some conditional. e.g. map! <expr> \bbq get(g:, 'use_mappings', 1) ? 'ℚ' : '\bbq'. Now you can do :let g:use_mappings = 0 to turn off the mappings. I also feel like these mappings should be abbreviations instead of mappings. Commented May 19, 2015 at 23:52
  • @PeterRincker I like to be able to search for α say by typing /\alpha. That works with map! but not ab.
    – Dan Piponi
    Commented May 20, 2015 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.