How to get easy nav window motions in evil-mode? - emacs

In vim I have this in my .vimrc:
" Easy window navigation
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
How do I get the easy window nav in evil-mode?
Also, in vim "+ will copy to my system clipboard? This doesn't seem to work in emacs.
$ emacs --version
GNU Emacs 24.3.1 Copyright (C) 2013 Free Software Foundation, Inc. GNU
Emacs comes with ABSOLUTELY NO WARRANTY. You may redistribute copies
of Emacs under the terms of the GNU General Public License. For more
information about these matters, see the file named COPYING.
$ cat .emacs
;;
;; Package manager and better repos
(require 'package)
(add-to-list 'package-archives
'("marmalade" .
"http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
(package-initialize)
(setq backup-directory-alist `(("." . "~/.emacs.d/saves")))
;; Indents, tab as spaces
(setq-default indent-tabs-mode nil)
(setq default-tab-width 2)
;; evil-mode
(eval-after-load "evil"
'(progn
(define-key evil-normal-state-map (kbd "C-h") 'evil-window-left)
(define-key evil-normal-state-map (kbd "C-j") 'evil-window-down)
(define-key evil-normal-state-map (kbd "C-k") 'evil-window-up)
(define-key evil-normal-state-map (kbd "C-l") 'evil-window-right)))
(require 'evil)
(evil-mode 1)
;(setcdr evil-insert-state-map nil)
;(define-key evil-insert-state-map [escape] 'evil-normal-state)
;; Fun stuff
(require 'jabber-autoloads)
(require 'jabber)

You can add the key bindings you want to use to evil-normal-state-map:
(eval-after-load "evil"
'(progn
(define-key evil-normal-state-map (kbd "C-h") 'evil-window-left)
(define-key evil-normal-state-map (kbd "C-j") 'evil-window-down)
(define-key evil-normal-state-map (kbd "C-k") 'evil-window-up)
(define-key evil-normal-state-map (kbd "C-l") 'evil-window-right)))
Wrapping the code into eval-after-load is necessary to ensure that evil-normal-state-map is defined/available when making the calls to define-key.
If you want to make the same bindings available in other "states" (such as "Motion" state) as well, just add them to the corresponding key maps as shown above (in the case of "Motion" state, the corresponding map is called evil-motion-state-map).
To get Emacs to use the system clipboard, try setting x-select-enable-clipboard to a non-nil value:
(setq x-select-enable-clipboard t)
There are also specific commands for killing and yanking that use the clipboard. From the documentation:
clipboard-kill-region:
Kill the region, and save it in the X clipboard.
clipboard-kill-ring-save:
Copy region to kill ring, and save in the X clipboard.

Related

key binding in slime emacs

I want to change the key binding M-x slime-fuzzy-complete-symbol to M-TAB in both slime-mode and slime-repl.
I looked up the common lisp manual, and tried the following sentences:
(eval-after-load 'slime
`(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol))
(add-hook 'slime-load-hook
#'(lambda ()
(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol)))
(define-key slime-repl-mode-map (kbd "M-TAB")
'slime-fuzzy-complete-symbol)
None of them is useful. The third sentence even have an error:
Symbol's value as variable is void: slime-repl-mode-map
For reference, the following the is my init.el relate to slime:
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
(normal-top-level-add-subdirs-to-load-path))
;; Setup load-path, autoloads and your lisp system
;; Not needed if you install SLIME via MELPA
(add-to-list 'load-path "~/default-directory/slime")
(require 'slime-autoloads)
(setq inferior-lisp-program "/usr/local/opt/clozure-cl/bin/ccl64")
;; Setup slime-repl
(setq slime-contribs '(slime-scratch slime-editing-commands))
;;(setq slime-contribs '(slime-repl)) ; repl only
(setq slime-contribs '(slime-fancy)) ; almost everything
;;Setup suto-complete
(add-to-list 'load-path "~/default-directory/auto-complete/")
(require 'auto-complete-config)
;;(add-to-list 'ac-dictionary-directories "~/default-directory/auto-complete/ac-dict")
(ac-config-default)
I know this is an old question, but a few weeks ago I was looking for an answer to a similar one and I hope additional info may be useful for someone...
Here's what I've found out:
using (eval-after-load 'slime) in your .emacs (dot-emacs) file is not a good idea until you have (autoload 'slime) in it - and this is OK - without that (eval-after-load "...") may wait till the end of time for loading slime
the previous answer is OK if global-key-binding is appropriate for you
if you require or prefere local binding you may define your own function and bind it locally, e.g.:
(defun my-slime-mode-keybindings ()
"For use in `slime-mode-hook' and 'slime-repl-mode-hook."
(local-set-key (kbd "<C-f1>") 'slime-describe-symbol)
(local-set-key (kbd "<M-f1>") 'slime-apropos-all)
(local-set-key (kbd "C-c C-p") nil) ;; when you want to remove a key/sequence
(local-set-key (kbd "C-<tab>") 'ace-window)
(local-set-key (kbd "M-<tab>") 'slime-fuzzy-complete-symbol) ;; your case :)
) ;; end of defun my-slime-mode-keybindings()
;; tell emacs to use your function only in required mode(s)
(add-hook 'slime-mode-hook #'my-slime-mode-keybindings)
(add-hook 'slime-repl-mode-hook #'my-slime-mode-keybindings)
best regards
It will be better to follow the emacs documentation for this case:
Key binding commands
In your case with a global key bindoing should work:
(global-set-key (kbd "M-TAB") 'slime-fuzzy-complete-symbol)

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.

Evil Emacs: is there a way to disable vim-like keys in «insert» mode?

I like VIM idea of text objects, so I installed EVIL (a Emacs plugin to emulate VIM features). But I'd like «insert» mode to leave Emacs keybindings unchanged (except perhaps Escape which is to switch into «normal» mode). Any way to achieve this?
By the way: ATM «insert» mode have a mixed set of hotkeys, which isn't very comfortable either way. E.g. the «M-b» works as in Emacs, but the «C-o» works as in VIM.
In the #emacs IRC channel I was told that someone already solved a similar problem. Here's the modified version I use:
(require 'evil)
;; remove all keybindings from insert-state keymap
(setcdr evil-insert-state-map nil)
;; but [escape] should switch back to normal state
(define-key evil-insert-state-map [escape] 'evil-normal-state)
(define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-normal-state-map (kbd "[ m") 'beginning-of-defun)
(define-key evil-normal-state-map (kbd "] m") 'end-of-defun)
(define-key evil-normal-state-map (kbd "k") 'evil-previous-visual-line)
(define-key evil-normal-state-map (kbd "j") 'evil-next-visual-line)
(evil-mode t)
(setq evil-jumps-cross-buffers nil) ;; for C-o and C-i to not cross buffers
(provide 'emvil)
The (provide 'emvil) is to allow require 'ing it in the configuration. I also found it useful to jump-to-definition in the next split screen unless the definition is in the buffer I'm currently in. Here's the code:
(defun evil-goto-definition-next-split ()
"If there's a free split, goto definition in this split,
otherwise use current one (except when a definition in the
current split)"
(interactive)
(let ((origin-spl (selected-window))
(origin-buf (current-buffer)))
(evil-goto-definition)
(when (and (eq origin-spl (selected-window)) ;; otherwise it's done
(not (eq origin-buf (current-buffer)))) ;; otherwise either definition not found, or
;; it's in the same buffer
(let ((defin-buf (current-buffer))
(defin-point (point)))
(switch-to-buffer origin-buf)
(other-window 1)
(switch-to-buffer defin-buf)
(goto-char defin-point)
))
))
(define-key evil-normal-state-map (kbd "g d") 'evil-goto-definition-next-split)

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.