48
votes

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.

7
So, what answer did you end up using?Alexandre Martins
See accepted answer: evil-want-C-u-scroll tjustingordon
With org-mode and related packages, C-u comes with lots of functionalities. But I get you, I want it to be scrolling up. Just curious if anyone knows how and what keybinding should we save the original emacs' C-u to?Student

7 Answers

54
votes

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

16
votes

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.

9
votes

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

6
votes

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.

1
votes

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.

1
votes

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

0
votes

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.