4 ms·
You want to create a minor mode (minor mode overrides major mode key bindings) and have the minor mode enabled globally. use-package has a bind-key* function th
by tabemacsthrow 5y ago
You want to create a minor mode (minor mode overrides major mode key bindings) and have the minor mode enabled globally. use-package has a bind-key* function that does this I think. Something like (check the syntax, I haven't tested it)
(require 'bind-key)
(bind-key* (kbd "tab") (lambda () (insert " ")))
And you should probably construct the number of spaces from the tab-width variable.
- akkartik 5y agoThanks! I'll look into this in combination with https://nullprogram.com/blog/2013/02/06 https://nullprogram.com/blog/2013/02/06 the next time I try out Emacs.
- tabemacsthrow 5y agoNo, bind-key* should do this for you automatically. If you wanted to do it yourself you would make a minor-mode manually. There's also general-override-mode-map from the "general" package. edit: Looking at the code I don't think bind-key* creates a minor-mode but it still does what you want (and is probably the recommended way).
- akkartik 5y agoOh! Let me try that.
- akkartik 5y agoHere's what I have so far: * I installed Emacs 26.3 using `sudo apt install emacs` * I installed Melpa by following https://github.com/melpa/melpa/blob/master/README.md#usage https://github.com/melpa/melpa/blob/master/README.md#usage * I installed use-package using `M-x package-install use-package` Now I'm trying to make sense of your instructions while reading https://github.com/jwiegley/use-package#readme https://github.com/jwiegley/use-package#readme. I don't see a bind-key*, but I do see: - bind-key - bind-keymap - bind-keymap* Should I try one of those instead? They all seem to be in the context of a `(use-package ...)` declaration. Edit: Never mind, I do see it now at https://github.com/jwiegley/use-package/blob/master/bind-key.el https://github.com/jwiegley/use-package/blob/master/bind-key... Edit 2: I just tried it and it didn't work. Turns out I've tried this approach in the past: https://lobste.rs/s/whaez0/emacs_everywhere#c_l6ez9w https://lobste.rs/s/whaez0/emacs_everywhere#c_l6ez9w (edit 2 there, heh) Wait, the suggestion at the top of that comment now works for me on a few different file extensions! (setq-default tab-width 2) (setq-default indent-tabs-mode nil) ; to insert a tab anyway: C-q TAB (define-key global-map [tab] (lambda () (interactive) (insert-tab))) Thank you!
- tabemacsthrow 5y agoI think the problem in your lobste.rs post is that you have (bind-key* "Tab" ...) instead of (bind-key* (kbd "tab") ...). I don't think your define-key solution works because I think it can be rebound by major or minor modes. I think bind-key.el is required by use-package so if you have use-package you should have it. If you don't have use-package you should install it. I'm not at a computer with an emacs installation so I can't try it out myself. I would try binding to a regular key (e.g. "q") while debugging to try to reduce the possible problems. I think you'll find that remapping tab everywhere like that is going to break a lot of things though (navigating fields/links, expanding sections, etc). You might want to just remap the key by explicitly listing the modes you care about. The #emacs channel on libera (previously freenode) used to be amazing but since IRC waned it's become less good but if you're lucky and during busy times you might still find someone there that can give you a better solution.
- akkartik 5y agoThanks a lot! Yeah, I'm already realizing I need to be careful what I wish for, because I can't tab complete anymore after C-x C-f. I think I'm going to follow the process outlined by CyberShadow: https://news.ycombinator.com/item?id=29726398#29728696 https://news.ycombinator.com/item?id=29726398#29728696. I'm not hung up on getting this perfectly automated, just want to not have to think about this the next time I run into it. I think I can now give Emacs another try!
- tabemacsthrow 5y agoSomeone above pointed out that you can remap it for prog-mode which (I think) programming modes derive from so (again, I think) if you remapped it there you would cover the programming modes. Then you could add the few other modes that aren't covered. You would define a list of the modes and loop over them in your init.el to remap them. The other thing you might want to try is to remap the function indent-for-tab-command to insert-tab.
- akkartik 5y ago