Emacs Meta-Key in Terminal - emacs

I'm trying to configure my Meta-Key, such that I can input {},[], etc. in a terminal emacs (using a German keyboard). I haved tried setting my Meta-Key, but can't get it to work. I want to use Alt+8 for {, Alt+9 for }, Alt-5 for [, Alt-6 for ].
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
See:
Unable to type braces and square braces in emacs

Have you tried?
(define-key global-map (kbd "M-8") "{")
(define-key global-map (kbd "M-9") "}")

For macos azerty French keyboard. It's :
(define-key global-map (kbd "M-(") "{")
(define-key global-map (kbd "M-)") "}")

Related

Evil in emacs: cannot remap "s": buffer is read-only

I'm trying to remap some evil keys to navigate using htns. htn works fine, but s always gives me the "buffer is read-only" error. I'm assuming I need to remap save buffer, but I'm getting lost in the key rebinding api. My .emacs looks like
(define-key evil-motion-state-map (kbd "n") 'evil-next-line)
(define-key evil-motion-state-map (kbd "s") 'evil-previous-line)
(define-key evil-motion-state-map (kbd "h") 'evil-forward-char)
(define-key evil-motion-state-map (kbd "t") 'evil-backward-char)
It's ok if IĀ use the normal state map instead:
(define-key evil-normal-state-map (kbd "s") 'evil-previous-line)
Does that suit your needs ?
I guess you had this error because the binding was not working.

Emacs evil: general window movement remap

This is the question I should have asked instead of this:
Emacs evil: space as a prefix key in motion state
I want to define a bunch of commands for moving, moving between, opening and closing windows and buffers that works in all states except insert mode, and are all of the form "SPC ". It would be nice to be able to set this once and be fine everywhere (except when there are conflicts, though overriding would be fine), but if that isn't easy, I would also like to know how to override keybindings in new states that I run across where my keybindings don't work. Hopefully knowing that would also help me edit keybindings in arbitrary states.
What I currently have is this:
(define-key evil-normal-state-map (kbd "SPC") nil)
(define-key evil-motion-state-map (kbd "SPC") nil)
(define-key evil-motion-state-map (kbd "SPC h") 'evil-window-left)
(define-key evil-motion-state-map (kbd "SPC j") 'evil-window-down)
(define-key evil-motion-state-map (kbd "SPC k") 'evil-window-up)
(define-key evil-motion-state-map (kbd "SPC l") 'evil-window-right)
(define-key evil-normal-state-map (kbd "SPC h") 'evil-window-left)
(define-key evil-normal-state-map (kbd "SPC j") 'evil-window-down)
(define-key evil-normal-state-map (kbd "SPC k") 'evil-window-up)
(define-key evil-normal-state-map (kbd "SPC l") 'evil-window-right)
and "SPC H" and so on for moving windows. It doesn't work in list-buffers or Dired. Evil leader only seems to work for normal mode.
We meet again.
Perhaps it'd be simpler to define a prefix keymap and bind to it. For example:
(define-prefix-command 'my-window-map)
(let ((map my-window-map))
(define-key map "h" 'evil-window-left)
(define-key map "j" 'evil-window-down)
(define-key map "k" 'evil-window-up)
(define-key map "l" 'evil-window-right)
(define-key map "H" 'evil-window-move-far-left)
(define-key map "J" 'evil-window-move-very-bottom)
(define-key map "K" 'evil-window-move-very-top)
(define-key map "L" 'evil-window-move-far-right)
;; And presumably, for opening/closing
(define-key map "v" 'evil-window-vsplit)
(define-key map "s" 'evil-window-split)
(define-key map "c" 'evil-window-delete))
Then you can map the prefix keymap to SPC in various modes:
;; Do this for each state you want these bindings available
(define-key evil-motion-state-map " " 'my-window-map)
(define-key evil-visual-state-map " " 'my-window-map)
;; You don't need to unbind/rebind evil-normal-state-map --
;; there is no default mapping for " ". Also: unbound keys in normal
;; mode will fall through to motion bindings.
;; For particular modes (like dired and list-buffer window)
(define-key dired-mode-map " " 'my-window-map)
(define-key Buffer-menu-mode-map " " 'my-window-map)
Alternatively, you can have dired and list-buffer start in normal mode. This will likely interfere with their default mappings.
(evil-set-initial-state 'dired-mode 'normal)
(evil-set-initial-state 'Buffer-menu-mode 'normal)
If you find your mapping overridden by another mode (which shouldn't be common for that key), you can try adding my-window-map to evil-overriding-maps: (add-to-list 'evil-overriding-maps '(my-window-map)). This supposedly gives those maps precedence.
Disclaimer: I haven't tested this. I find it simpler to undefine keys in conflicting plugins.
On a side note, all these commands are already available in evil-window-map. It may be simpler for you to map SPC to that:
(define-key evil-motion-state-map " " 'evil-window-map)
(define-key evil-visual-state-map " " 'evil-window-map)
...

How to keep traditional binding on M-x in emacs evil mode

I am trying to bind execute-extended-command to M-x in evil normal mode.
I currently have
;; evil mode
(require 'evil)
(evil-mode 1)
(define-key evil-normal-state-map "M-x" 'execute-extended-command)
in my .emacs file but the keybinding doesn't work. I tried replacing
"M-x"
with
"\M-x"
and
(kbd "M-x")
but neither works. I also tried adding it to evil.el and evil-maps.el.
I don't know what's wrong with your binding. You could use Emacs' own global-set-key for global stuff and if you plan something special for say, insert mode, you could override that later on, like this:
;; this works, just tested. My evil is 1.0-dev from github.
(global-set-key (kbd "M-x") 'smex)
(define-key evil-insert-state-map (kbd "M-x") 'execute-extended-command)
Use (kdb "") macro when you have modifier keys in your binding. But you can use the macro always, regardless of the content. These are for example usage. When in doubt, wrap the key in (kdb ).
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;;(global-set-key (kbd "M-x") 'execute-extended-command)
(define-key evil-normal-state-map ",d" 'volatile-kill-buffer)
(define-key evil-normal-state-map ",b" 'ido-switch-buffer)
(define-key evil-normal-state-map ",s" 'ispell-word)
(define-key evil-normal-state-map (kbd "C-x g") 'magit-status)
(define-key evil-insert-state-map (kbd "C-f") 'my-expand-file-name-at-point)
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
(define-key minibuffer-local-map (kbd "C-w") 'backward-kill-word)
(define-key evil-normal-state-map (kbd ",ff") 'ido-find-file)
After long research and with help from #emacs and #evil-mode channels on irc, it turned out that my emacs was broken. It was a snapshot from http://emacs.naquadah.org/ I tried all this on another emacs version (from debian jessies repos) and it worked ok.

Ctrl+U in emacs when using evil key bindings

I'm using the wonderful evil package for vim bindings in emacs.
The one key that is not right is Ctrl+U. It is still the emacs prefix, rather than "up".
Does anybody have a solution for that in some lisp code for my .emacs?
Thanks.
there is a variable that you can add to your .emacs
(setq evil-want-C-u-scroll t)
it needs to appear before the call to (require 'evil).
Alternatively, it's easy enough to define your own keybindings, and the evil API is rich enough to make it super easy:
(define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-visual-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-insert-state-map (kbd "C-u")
(lambda ()
(interactive)
(evil-delete (point-at-bol) (point))))
I had to go this route as evil-want-C-u-scroll wasn't functioning correctly for me.
In order to get bling's answer to work for anyone useing John Wiegley's use-package, make sure you define it in the :init section, like so:
(use-package evil
:ensure t
:init
(setq evil-want-C-u-scroll t)
:config
(evil-mode 1)
;; snip...
)
HTH
Vim's C-u is half-screen page up. I replicated it using the following,
(define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)
From C-h f evil-scroll-up,
(evil-scroll-up COUNT)
Scrolls the window and the cursor COUNT lines upwards.
The default is half the screen.
The vim's C-u is not 'previous-line, it's more like page up. I don't know how to replicate the exact behavior, but you could just try C-b (evil-scroll-page-up) or map C-k, C-j to go up/down 10 lines.
(global-set-key (kbd "C-k") (lambda () (interactive) (previous-line 10)))
(global-set-key (kbd "C-j") (lambda () (interactive) (next-line 10)))
The C-u key is also quite important to Emacs so you probably shouldn't overwrite it anyway.
To add to melleb's answer, I also defined the key combination when evil-want-C-u-scroll:
(use-package evil
:ensure t
:init
(setq evil-want-C-u-scroll t)
(when evil-want-C-u-scroll
(define-key evil-insert-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-visual-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-motion-state-map (kbd "C-u") 'evil-scroll-up))
:config
(evil-mode 1)
...
)
This works for GNU Emacs 24.4.1
First, to answer your question:
(define-key evil-insert-state-map "\C-u" 'previous-line)
(define-key evil-normal-state-map "\C-u" 'previous-line)
(define-key evil-replace-state-map "\C-u" 'previous-line)
(define-key evil-visual-state-map "\C-u" 'previous-line)
(define-key evil-motion-state-map "\C-u" 'previous-line)
Since I can't really test myself (no evil), try maybe the following if those do not work:
Replace
(define-key evil-motion-state-map "\C-u" 'previous-line)
With
(define-key evil-motion-state-map "cu" 'previous-line)
Do this for whichever mode of evil you want it/it is neccessary.
Furthermore, maybe there is an "evil" version of up, you might want to bind that instead.
Also, correct me if I am wrong, but I am pretty sure evil 'ships' with a functional/useful "up" somewhere in those keybindings, maybe read up on it somewhere.

Useful keyboard shortcuts and tips for ESS/R

I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.
I have set several shortcuts in my .emacs file. The most useful are:
C-tab to switch between the R command line and the file (similar to josh answer, but much faster):
(global-set-key [C-tab] 'other-window)
Control and up/down arrow keys to search history with matching what you've already typed:
(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)
Comment-uncomment a selected region with C-d or C-maj-d
(defun uncomment-region (beg end)
"Like `comment-region' invoked with a C-u prefix arg."
(interactive "r")
(comment-region beg end -1))
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):
;; Delete selection when pressing [delete] key
(delete-selection-mode t)
;; ESS Mode (.R file)
(define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
(define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
(define-key ess-mode-map "\C-r" 'ess-eval-region)
;; iESS Mode (R console)
(define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
(define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
(define-key inferior-ess-mode-map "\C-a" 'comint-bol)
(define-key inferior-ess-mode-map [home] 'comint-bol)
;; Comint Mode (R console as well)
(define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
(define-key comint-mode-map "\C-r" 'comint-show-output)
(define-key comint-mode-map "\C-o" 'comint-kill-output)
;; Search with C-f / C-F (control-maj-F for backware search)
(global-set-key "\C-f" 'isearch-forward)
(global-set-key (kbd "C-S-f") 'isearch-backward)
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)
;; Save with C-s / C-S
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'write-file)
;; need to redefine them for isearch mode (don't know why)
(define-key isearch-mode-map (kbd "C-s") 'save-buffer)
(define-key isearch-mode-map (kbd "C-S-s") 'write-file)
;; Pause = dedicate window.
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
;; delete = delete
(global-set-key [delete] 'delete-char)
;; C-b = list buffers
(global-set-key (kbd "C-b") 'bs-show)
You will find many more useful shortcuts in ESS documentation.
C-c C-z ess-switch-to-end-of-ESS
is nice to jump from your source file that you are editing foo.R to the R console
I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.
http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
M-n and M-p in the ESS R console for next/previous command.