51

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.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
justingordon
  • 12,553
  • 12
  • 72
  • 116

7 Answers7

59

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

bling
  • 1,121
  • 11
  • 8
17

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.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • Almost perfect... C-u in Vim deletes to the current indent on first press, then deletes the indent on second press. – Aron Griffis Feb 09 '15 at 19:39
10

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

Community
  • 1
  • 1
Melle
  • 7,639
  • 1
  • 30
  • 31
8

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.

sharat87
  • 7,330
  • 12
  • 55
  • 80
  • I think this answer is incomplete - this won't enable C-u in modes other than Normal mode. – Rory O'Kane May 07 '13 at 22:28
  • `C-u` already has this behavior in visual mode. In insert mode, in vim, it does't scroll up, it deletes back to start of line/entry into insert. I'm not sure how you'd replicate that behavior in emacs. – sharat87 May 08 '13 at 03:44
  • You're right, redefining `C-u` as in the answer also affects Visual mode. I had assumed it wouldn't because `evil-visual-state-map` wasn't updated, but I guess that's unnecessary for some reason. – Rory O'Kane May 08 '13 at 18:36
  • I think you assumed correct. Its just that `C-u`'s default behavior in visual mode is already correct. No need to define it. – sharat87 May 08 '13 at 19:15
  • I don't think `C-u`'s default behavior is to scroll up in visual mode. I disabled all key mappings and Evil-mode customizations in my `.emacs` and then tried `C-u` in both normal mode and visual mode, and in both cases, it displays `C-u-` in the minibuffer and waits for an argument. And `C-h c C-u` in both cases displays "`C-u` runs the command `universal-argument`". Only after I `M-x eval-expression` and paste in your `define-key` code does `C-u` make the buffer scroll up. I'm running Evil 1.0.1, the latest version. – Rory O'Kane May 08 '13 at 19:23
  • Yep, you're right. My `define-key` does set `C-u` in visual mode too. Thanks for correcting me :) – sharat87 May 09 '13 at 04:33
1

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.

tungd
  • 14,467
  • 5
  • 41
  • 45
1

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

Community
  • 1
  • 1
tomcat
  • 219
  • 3
  • 9
0

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.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160