Navigate up & down in / search history when evil-set-initial-state is set to 'emacs ?

John Magolske listmail at b79.net
Sat Mar 18 08:45:22 CET 2017


Hi Vasilij,

Thanks for the reply & useful info!

* Vasilij Schneidermann <v.schneidermann at gmail.com> [170315 08:50]:
> > For some modes I find it more convenient to be in Emacs state and add
> > some key Evil mappings as needed, like so:
> > 
> >     ;; start the Package Menu in Emacs mode, add some Evil mappings
> >     (evil-set-initial-state 'package-menu-mode 'emacs)
> >     (evil-add-hjkl-bindings package-menu-mode-map 'emacs
> >       (kbd "/")       'evil-search-forward
> >       (kbd "n")       'evil-search-next
> >       (kbd "N")       'evil-search-previous)
> 
> Interesting, for Emacs state bindings I'd have modified the underlying
> keymap directly like so:
> 
>     (with-eval-after-load 'package
>       (define-key package-menu-mode-map (kbd "/") 'evil-search-forward) ...)

Seems evil-add-hjkl-bindings is similar, but adds the hjkl mappings
in for starters. I modified that macro to include a few more mappings
that I find myself wanting for general navigation:

;; Add hjkl and other vi bindings...
(defmacro evil-add-hjkl-bindings-plus (keymap &optional state &rest bindings)
  "Add \"h\", \"j\", \"k\", \"l\", \"\/\", \"n\", \"N\", \"e\", \"b\" and other bindings to KEYMAP in STATE.
Add additional BINDINGS if specified."
  (declare (indent defun))
  `(evil-define-key ,state ,keymap
     "h" (lookup-key evil-motion-state-map "h")
     "j" (lookup-key evil-motion-state-map "j")
     "k" (lookup-key evil-motion-state-map "k")
     "l" (lookup-key evil-motion-state-map "l")
     ":" (lookup-key evil-motion-state-map ":")
     "/" 'evil-search-forward
     "n" 'evil-search-next
     "N" 'evil-search-previous
     "e" 'evil-forward-word-end
     "b" 'evil-backward-word-begin
     "DEL" 'scroll-down-command
     "SPC" 'scroll-up-command
     "q" '(lambda () (interactive) (quit-window t))
     , at bindings))

That way I don't have to re-type all that across multiple modes:

(evil-set-initial-state 'package-menu-mode 'emacs)
(evil-add-hjkl-bindings-plus package-menu-mode-map 'emacs)
(evil-set-initial-state 'paradox-menu-mode 'emacs)
(evil-add-hjkl-bindings-plus paradox-menu-mode-map 'emacs)
(evil-set-initial-state 'help-mode 'emacs)
(evil-add-hjkl-bindings-plus help-mode-map 'emacs)
(evil-set-initial-state 'eww-mode 'emacs)
(evil-add-hjkl-bindings-plus eww-mode-map 'emacs)
(define-key isearch-mode-map (kbd "<up>") 'isearch-ring-retreat)
(define-key isearch-mode-map (kbd "RET") 'isearch-exit)

> > [...] I'm not finding a way to Navigate up & down in the search
> > history initiated by / ('evil-search-forward). I have in my init
> > file this:
> > 
> >     (setq evil-search-module 'evil-search)
> > 
> > Which allows such navigation through search history -- but only when
> > in Evil vi states, not when evil-set-initial-state is set to 'emacs.
> > 
> > Any suggestions re how to achieve this in Emacs state?
> 
> Could you please elaborate on the exact workflow?  This sounds to me as
> if you want to initiate a search with `evil-search-forward`, then switch
> out the currently entered search term (which can be done with `M-p` and
> `M-n` already) and continue your session.  These keys are bound to
> `isearch-ring-retreat` and `isearch-ring-advance`.

Yes, for example (evil-set-initial-state 'package-menu-mode 'emacs)
has the package menu coming up in Emacs/non-Evil state, which allows
more of the package's bindings to come through. But there are a few Vi
navigational functions I'd like to retain. And indeed, M-p and M-n do
what I want here, thanks for cluing me in. I made some bindings like:

    (define-key isearch-mode-map (kbd "<up>") 'isearch-ring-retreat)
    (define-key isearch-mode-map (kbd "RET") 'isearch-exit)

The behaviour is a bit different than what the evil-search search
module provides, but it's close enough.

Best regards,

John

-- 
John Magolske
http://b79.net/contact



More information about the implementations-list mailing list