0

I want to enter tab literally by control + v . But, control + v is already emacs key-binding.

How can I invalidate a key-bindings temporally ?

2
  • You also have Ctrl-q for the same exact purpose (you even have that in Vim! as a Ctrl-v alternative).
    – VanLaser
    Commented Jun 5, 2016 at 11:36
  • 1
    It works! thank you! honestly speaking, that is my purpose
    – tsrrhhh
    Commented Jun 5, 2016 at 11:36

2 Answers 2

1

You don't need to invalidate a keybinding to reassign it. You can just assign it as normal, and there are many questions & answers here about how to do it.

To change a key-binding 'temporarily' (actually locally), you have a few options:

  1. Change the binding only for one buffer, via (local-set-key ...). This is a permanent change, but only for the buffer you call it in.,
  2. Change it in a particular major or minor mode (such as org-mode, cc-mode etc) by using (define-key KEYMAP KEY DEF), where KEYMAP is the keymap for the mode you want to alter, KEY is the keybinding, and DEF is the function you want it to call. This is also a permanent change, which will be in effect only for buffers where that mode is used.
1
  • 1
    local-set-key changes the map the buffer is currently associated with. This is often the major-mode-map so using it will then change the binding in all buffers with that same major mode.
    – clemera
    Commented Jun 6, 2016 at 16:16
1

As commented, local-set-key changes the local map, which in most cases is a map from some major mode. If you really want a binding that affects only the current buffer, you need to make sure that it has one that is not used elsewhere. AFAIK, there is no such function in Emacs, but I've had the following hack for years to do it. It defines a buffer-local-set-key command to get a truly buffer-local binding (and a buffer-local-unset-all to undo all such local bindings):

(defvar buffer-local-keymap nil)
(make-variable-buffer-local 'buffer-local-keymap)

(defun buffer-local-set-key (key command)
  "Same as `local-set-key' but ensures the current buffer has its own map."
  (interactive "KSet key buffer-locally: \nCSet key %s locally to command: ")
  (unless buffer-local-keymap
    (setq buffer-local-keymap (make-sparse-keymap))
    (when (current-local-map)
      (set-keymap-parent buffer-local-keymap (current-local-map)))
    (use-local-map buffer-local-keymap)
    (message "NOTE: buffer-local keymap in effect, %s"
             "local-key operations will use it from now"))
  (local-set-key key command))

(defun buffer-local-unset-all ()
  "Undoes all bindings that `buffer-local-set-key' did in this buffer."
  (interactive)
  (message (if (not buffer-local-keymap)
             "Wasn't using a local map."
             (progn (use-local-map (keymap-parent buffer-local-keymap))
                    (setq buffer-local-keymap nil)
                    "Removed `buffer-local' bindings."))))

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.