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.


Solution 1:

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

Solution 2:

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.

Solution 3:

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

Solution 4:

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.

Solution 5:

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.