Bind C-z in evil mode to escape to shell - emacs

In Emacs evil mode, the key combo C-z is to toggle evil mode. I would like to rebind it to escape to shell instead. How would I do this ?
I have read about eshell, it seems to be great, but for now I would like to work with my zsh shell first.
Multi term seems to designed for this job, but I think escaping to shell is fine for me, since I'm used to this flow in Vim.
Thanks for reading.

Perhaps what you need is C-x C-z.

Just have the same requirement, and here's my configurations:
(add-to-list 'load-path "~/.emacs.d/evil")
(add-to-list 'load-path "~/.emacs.d/evil/lib")
(setq evil-toggle-key ""); remove default evil-toggle-key C-z, manually setup later
(require 'evil)
(evil-mode 1)
;; remove all keybindings from insert-state keymap, use emacs-state when editing
(setcdr evil-insert-state-map nil)
;; ESC to switch back normal-state
(define-key evil-insert-state-map [escape] 'evil-normal-state)
Ref:
1. https://gist.github.com/kidd/1828878
2. https://askubuntu.com/questions/99160/how-to-remap-emacs-evil-mode-toggle-key-from-ctrl-z

C-x C-z will suspend the frame and return you to the shell.
C-z as you mention toggles evil mode on/off.
I swap their behavior in evil like so:
(define-key evil-motion-state-map (kbd "C-z") 'suspend-frame)
(define-key evil-emacs-state-map (kbd "C-z") 'suspend-frame)
(define-key evil-motion-state-map (kbd "C-x C-z") 'evil-emacs-state)
(define-key evil-emacs-state-map (kbd "C-x C-z") 'evil-exit-emacs-state)
See this commit for an example (where I also make C-z emulate vim-behavior in insert/replace mode).

Related

Minor Mode Conditional Emacs Shortcuts

I have an emacs shortcut set up like so:
;; Adding the key mappings to minor mode.
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
;; Cancel with one press of escape instead of three.
(define-key my-keys-minor-mode-map (kbd "<escape>") 'keyboard-quit)
(my-keys-minor-mode 1)
(defconst my-minor-mode-alist (list (cons 'my-keys-minor-mode
my-keys-minor-mode-map)))
(setf emulation-mode-map-alists '(my-minor-mode-alist))
This works great to override a shortcut in a truly global way, superseding all other minor modes. However I need a way to have a shortcut behave differently in different minor modes. For example I would like the escape key to run helm-keyboard-quit when helm is running bu run keyboard-quit otherwise. Doing so like:
(eval-after-load "helm"
'(progn
(define-key helm-map (kbd "<escape>") 'helm-keyboard-quit)))
won't work because the global escape shortcut will override it. Is there a way to do this?
Why not use a simple lambda. Something like this (untested):
(global-set-key (kbd "<escape>")
(lambda()
(interactive)
(if helm-mode (helm-keyboard-quit)
(keyboard-quit))))

Use 'execute-extended-command' from Evil command mode (M-x is undefined)

I just started experimenting with emacs. And as a Vim user I did not want to bother use it without installing evil-mode first.
However, evil-mode seems to break the emacs keybinding for 'execute-extended-command' (M-x).
I really don't care about this keybinding, and I noticed that I can call for example 'list-packages' from the evil command-mode and it works just fine.
Only thing I am missing now, is the TAB auto completion in command mode, like typing 'list-' TAB and then it will show or iterate available commands.
Is this possible with evil-mode?
or is there eventually an easy way to fix this keybinding?
I am using:
debian jessie
GNU Emacs 24.4.1
EDIT:
My .emacs file:
(require 'package)
(push '("marmalade" . "http://marmalade-repo.org/packages/")
package-archives )
(push '("melpa" . "http://melpa.milkbox.net/packages/")
package-archives)
(add-to-list 'load-path "~/.emacs.d/evil")
(require 'evil)
(evil-mode 1)
(define-key evil-normal-state-map [escape] 'keyboard-quit)
(define-key evil-visual-state-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
I attempted to add Modifier-x
(global-set-key (kbd "C-x") 'execute-extended-command)
and Alt-x
(global-set-key (kbd "A-x") 'execute-extended-command)
but these attempts didn't fix the keybinding, so I've decided to roll with super key
(global-set-key (kbd "s-x") 'execute-extended-command)
Fixing keyboard bindings is quite easy - just add this line to your .emacs file (or evaluate it in the scratch buffer):
(global-set-key (kbd "< put your key combination here >") 'execute-extended-command)
You can use reference from ErgoEmacs to understand how to provide key combinations for kbd function.
And IMO it's better to use standard Emacs means to run commands - use your evil-mode command mode for evil-related things, and use M-x/execute-extended-command to work with Emacs.

Minor mode overriding another minor mode key binding

I installed Jedi mode in Emacs. I noticed that it overrides C-c . and C-c , (goto-definition and goto-definition-pop-marker respectively).
Here is how I set it up in my init file:
(setq jedi:setup-keys t)
(add-hook 'python-mode-hook 'jedi:setup)
I am using another mode called multiple-cursors that is set up as follows:
(add-to-list 'load-path "~/.emacs.d/multiple-cursors.el/")
(global-set-key (kbd "C-c .") 'mc/mark-next-like-this)
(global-set-key (kbd "C-c ,") 'mc/mark-previous-like-this)
Once jedi-setup loads, it rebinds my C-c . and C-c ,. What I'd like to do is keep my multiple cursors bindings and remap Jedi's bindings. I tried adding this to the end of my Jedi setup but it ends up mapping the Jedi functions to both C-c./, and C-c j/k at the same time.
(setq jedi:key-goto-definition (kbd "C-c k"))
(setq jedi:key-goto-definition-pop-marker (kbd "C-c j"))
The reason this happens is because Jedi binds them on the regular C-c ./, mappings, then just adds another C-c j/k mapping.
How do I stop Jedi from completely not binding to the C-c ./, and only bind to C-c j/k?
Unbind the keys you want in the Jedi mode keymap: just bind them to nil in that map.
Or change the order of the entries in minor-mode-map-alist.
See (elisp) Controlling Active Maps.

Emacs elisp: How to change a keybind for a specific mode in evil's evil-insert-state-map

I've been using emacs for a little while now and am still trying to get the hang of elisp. In my init.el, I have the following lines:
(define-key evil-insert-state-map (kbd "RET") 'newline-and-indent)
(add-hook 'org-mode-hook (lambda () (define-key evil-insert-state-map (kbd "RET") 'newline)))
The intended effect of these two lines of elisp is to disable automatic indentation in org-mode only, but keep automatic indentation for every other mode. However, while this code does disable automatic indentation for org-mode, it has the unintended effect of disabling it for everything else as well. Does anyone know of a way to achieve the desired effect?
You're looking for evil-define-key:
(evil-define-key 'insert org-mode-map (kbd "RET") 'newline)
This will define return to call newline in insert state only in org-mode. What your hook was doing was redefining the global insert state map every time you opened an org buffer.

emacs, flyspell, deactivate "C-." key binding

I have this little problem, I have some key bindings like so C-. C-x or C-. C-m. After I activate the flyspell-mode, I cannot use these commands. In my .emacs file I have the next 2 lines before
(global-unset-key (kbd "C-."))
(define-key (current-global-map) (kbd "C-.") nil)
(global-set-key (kbd "C-. C-l") 'global-linum-mode)
Then, my C-. C-l works, but it does not when the flyspell-mode is activated. The command bound to C-. is flyspell-auto-correct-word. I tried to deactivate it as follows:
;; first try
(defun flyspell-auto-correct-word-disable() (define-key (current-local-map) (kbd "C-.") nil))
(add-hook 'flyspell-mode-hook 'flyspell-auto-correct-word-disable)
;; second try
(define-key (current-global-map) [remap flyspell-auto-correct-word] nil)
None of the tries work, what can I do? I tried in Emacs 23 and 24 and I have the same issue.
What about:
(eval-after-load "flyspell"
'(define-key flyspell-mode-map (kbd "C-.") nil))
Your first solution is almost correct, but you have to remember that the current local map is set up by the major mode, not minor modes. The best option you have it to directly access flyspell-mode-map and modify it (another option would be to find it in minor-mode-map-alist but I think it would be needlessly complicated).
Also, I prefer putting such mode-specific settings within eval-after-load (which means they will be evaluated once) rather than in a hook (in which case the settings are evaluated multiple times: each time one buffer activates flyspell-mode). But this is a matter of preference and either way is fine.