reddit / emacs

Why doesn't my binding for C-' work in my notes.org file, but it does work in my init.el file?

Status-Detective-783 · 2022-03-10 03:47
I put what I thought was necessary in init.el to bind `C-'` to `avy-goto-char-2`. And it does bind within init.el, I can use it within init.el. But it's not bound when I switch to my notes.org buffer. Instead, it's bound to: C-' runs the command org-cycle-agenda-files (found in org-mode-map), Here is what I think is the relevant section of my init.el, which comes after the org mode section: (require 'avy) ;; Integrate avy with iSearch ;; First enter iSearch, then whatever pattern to search, then C-' ;; C-s PATTERN C-' ;; Note that the C-' is bound here ONLY in the context of an interactive iSearch (avy-setup-default) ;; not sure what this does ;; (global-set-key (kbd "C-c C-j") 'avy-resume) ;; Suggested bindings from avy github docs (global-set-key (kbd "C-:") 'avy-goto-char) (global-set-key (kbd "C-'") 'avy-goto-char-2) (global-set-key (kbd "M-g f") 'avy-goto-line) (global-set-key (kbd "M-g w") 'avy-goto-word-1) (global-set-key (kbd "M-g e") 'avy-goto-word-0) Here's my entire init.el, in case it's needed: https://0bin.net/paste/eFN-zR0r#sHQj8-kT3S49IpuH+vEPxpdAaZU6b+guUOoDxCDav1r
flexibeast · 2022-03-10 04:55
It's due to Emacs' notion of "active keymaps"; [this section of the Emacs Lisp Reference Manual](https://emacsdocs.org/docs/elisp/Active-Keymaps) has the details. Excerpt: > Usually, the active keymaps are: (i) the keymap specified by the keymap property, (ii) the keymaps of enabled minor modes, (iii) the current buffer’s local keymap, and (iv) the global keymap, in that order. Emacs searches for each input key sequence in all these keymaps.
Status-Detective-783 · 2022-03-10 07:36
Thanks! I'm still not sure how to get `C-'` bound to `avy-goto-char-2` in org-mode, though. It seems very complicated to me.
flexibeast · 2022-03-10 08:05
To override org-mode's default binding for `C-'`, you can do something like: (add-hook 'org-mode-hook '(lambda () (define-key org-mode-map (kbd "C-'") #'avy-goto-char-2)))
SlowValue · 2022-03-10 13:33
No need to do that on every org buffer. It would be enough to do it after "org.el" has been loaded. (with-eval-after-load 'org (define-key org-mode-map (kbd "C-'") #'avy-goto-char-2)) Also, I would rather unbind `C-'` from `org-mode-map` so this keybinding can fall through to the next keymap (which hopefully is global-keymap). The benefit of this: when changing mapping of `C-'` in global map, you would not need to also change this keybinding for `org-mode-map`. (with-eval-after-load 'org (define-key org-mode-map (kbd "C-'") nil)) and here it is for `use-package` users: (use-package org :bind (:map org-mode-map ("C-'" . nil)))
Status-Detective-783 · 2022-03-10 13:35
Thanks! That worked! (Which should be no surprise to you, but it was to me.) I really appreciate it. And I'll strive to learn its meaning when I get to that point in elisp.
Status-Detective-783 · 2022-03-10 15:24
Very interesting, thanks! Question. Would the (require org) use the same form as your (use-package org...)? My understanding, iirc, is that require calls use-package (or some kind of "load" command) only once, even if duplicated somewhere, so it's just a little safer.
oantolin · 2022-03-10 19:22
In the use-package form I prefer `("C-'")`, because it looks funny. (Not sure "looking funny" is how you're supposed to choose your code style, but I only use it in my personal configuration. :P)
SlowValue · 2022-03-10 19:54
I'm not sure, that I understand your question. `use-package` and `require` are different and have different forms (signatures). `require` is a function written in c, which loads an elisp file. `use-package` is a Lisp Macro, which handles a couple of complex elisp calls through a nice user interface (a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language)). `use-package` utilizes `require` in order to fulfill its duties. `require` does not call `use-package`. A `use-package` form is called every time it is evaluated (e.g. it is found in init.el), but sometimes a `use-package` form generates a `eval-after-load` form, and that form is called only once (after the elisp file has been loaded). But the rest of the `use-package` form is evaled right at the spot. Lisp Macros are very powerful, because they implement [Metaprogramming](https://en.wikipedia.org/wiki/Metaprogramming) and are therefore able to generate valid lisp code.
Status-Detective-783 · 2022-03-10 20:04
I think in that case, your answer is "No." Haha. > I'm not sure, that I understand your question. It's me. I clearly don't understand the emacs mechanisms enough to ask a more coherent question. I'll just use your working code for now (Thanks again!), and research/learn-about these topics in more depth when the time comes.
SlowValue · 2022-03-10 20:22
When you understand, you will love emacs, because it makes it soo easy and fun to mod it. Reading a good introductionary book like ["Mastering Emacs"](https://www.masteringemacs.org/book) or ["Practical Common Lisp"](https://gigamonkeys.com/book/) helps a lot understanding Lisp and Emacs. There is also an [Elisp Intro](https://www.gnu.org/software/emacs/manual/html_node/eintr/), but I haven't read it.