How can i remap a non-ascii keyboard character in evil-mode? - emacs

I would like to remap the key "é" on my keyboard to ctrl-w in emacs evil-mode (doom emacs)
I tried with
(define-key evil-operator-state-map "é" "\C-w")
;; and
(global-set-key "é" "\C-w")
But none of these worked.
Is what I'm trying to do possible ?
How can I do it ?

Try this:
(define-key key-translation-map (kbd "é") (kbd "C-w"))
Use (kbd KEY-SEQUENCE), where KEY-SEQUENCE is what Emacs tells you the key sequence is.
Use key-translation-map to translate keys. See the Elisp manual, node Translation Keymaps.

Related

Bind C-z in evil mode to escape to shell

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).

Emacs weird behaviour with key

I'm a Vim user, and I decided to give Emacs a try.
Now I get a weird problem with Emacs. I installed the Evil mode, which is awesome.
In my .emacs I have the follow setting:
(define-key evil-motion-state-map "\C-u" 'scroll-up-command)
This works well.
But what if I wanted to change it to spacebar or Return key?
(define-key evil-motion-state-map "<return>" 'scroll-up-command)
(define-key evil-motion-state-map "SPC" 'scroll-up-command)
Nothing spectular will happen, the return/Enter key and spacebar are behaving their standard behaviour. I looked around for the right keys, and it seems they're the rights keys. For example,
(global-set-key (kbd "<return>") 'save-buffer)
Works fine.
What am I doing wrong in Emacs/Evil with the Enter key?
Use (kbd "<return>"), not "<return>". Likewise, (kbd "SPC").
You probably want (kbd "RET"), not (kbd "<return>").
Binding C-u, as you did at first, is a bad idea. You do not want to do that, ever. Just use C-u as it was intended, for command universal-argument -- see the Emacs manual, node Arguments.

Binding C-; in Emacs

How can I bind a function to C-; in Emacs? I tried to use bracket notation with an escape character:
(global-set-key [C-\;] 'my-func)
and kbd:
(global-set-key (kbd "<C-;>") 'my-func)
Is it not possible? If so, then why acknowledge the existence of the chord (C-; is undefined)?
I am running Emacs 24.1.1.
(global-set-key (kbd "C-;") 'my-func) seems to avoid problems with the comment character and escaping.

How to bind text insertion in isearch

I'd like to have M-u to insert an underscore when I am in isearch (isearch-regexp and also the reverse variants).
Neither
(define-key isearch-mode-map (kbd "M-u") 'insert-underscore)
nor
(add-hook 'isearch-mode-hook
(lambda ()
(local-set-key (kbd "M-u") 'insert-underscore)
))
insert-underscore is my function that simply inserts "_". It works in the main frame and also in minibuffer, but I can't get it working in isearch...
Thank you!
Isearch doesn't use regular commands. (kbd "_") along with every other
printable character is bound to a special command in isearch-mode-map. It's
not obvious, but a lot of things happen in "isearch-mode" when you press a
key. Display is refreshed with new results, wrapping is a possibility, etc, etc,
You'd have to manipulate raw keyboard events to get this to work.
(defun underscore ()
(interactive)
(isearch-unread-key-sequence (list ?_)))
(define-key isearch-mode-map (kbd "M-u") 'underscore)
Note that this code is not robust; for example, numeric prefix does not work.
EDIT: After letting percolate in my mind for a while, it occured to me that this is the exact use-case for translation keymaps
(define-key key-translation-map (kbd "M-u") (kbd "_"))
Ain't Emacs grand?

Emacs Ctrl modifiers don't work in console

I have 2 hotkeys for dired, that work in GUI mode in Emacs:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>")
(lambda () (interactive) (find-alternate-file "..")))))
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<right>") 'diredp-find-file-reuse-dir-buffer)))
But when I click CTRL+→ or CTRL+↑ in console the cursor just moves as if the arrow was pressed.
When I try CTRL+H K and then CTRL+→, it gives me the right key docs as if CTRL wasn't pressed at all.
How to fix this strange behaviour in console?
I am using Linux Slackware 14, Emacs 24.2.1.
Here is the algorithm, how to make modifier keys work in Emacs in terminal.
1.Create a file funcskeys with content:
control keycode 105 = F100
string F100 = "\033[1;5D"
control keycode 106 = F101
string F101 = "\033[1;5C"
control keycode 103 = F102
string F102 = "\033[1;5E"
altgr keycode 105 = F103
string F103 = "\033[1;5F"
At the end must be an empty line!
2.Under root load the file:
#loadkeys funcskeys
3.Put this into the beginning of .emacs:
(unless (display-graphic-p)
(progn
(define-key input-decode-map "\e[1;5C" [(control right)])
(define-key input-decode-map "\e[1;5D" [(control left)])
(define-key input-decode-map "\e[1;5E" [(control up)])
(define-key input-decode-map "\e[1;5F" [(meta left)])))
End of algorythm
After this hotkeys will work:
(global-set-key (kbd "C-<right>") 'forward-word)
(global-set-key (kbd "C-<left>") 'backward-word)
Your terminal likely doesn't produce different escape sequences for CTRL-right‬ versus just right.
You can easily verify this by typing CTRL-v CTRL-right‬ and CTRL-v right‬. Here, CTRL-v tells the terminal to print the escape sequence for the key that follows. If these two produce the same sequences then your terminal sends the exact same information to Emacs whether you press CTRL or not.
For instance, if I type these shortcuts in a Gnome terminal, I get:
^[[C for CTRL-v right‬
^[[1;5C for CTRL-v CTRL-right‬
When I do the same on one of the Linux consoles, I get:
^[[C for CTRL-v right‬
^[[C for CTRL-v CTRL-right‬
As you can see, in the latter case it's exactly the same result for both key sequences, hence there's no way Emacs can distinguish the two.
The only way to fix this is to convince your terminal to send a different sequence when you hold down the CTRL key - see this question for more information.
An easier work-around would be to simply use different key bindings in Emacs.
Look out for loadkeys. At least in Debian/Ubuntu it's in the package kbd. With it, you can modify your keyboard layout and probably also some more "exotic" key combinations.