Is there a way to do a history search in nrepl?
Asked Answered
H

3

6

You know how when you hit the up arrow in bash it will fill in the last command you typed in? Is there any way to do this in nrepl?

So far I've been doing a reverse search (C-r), typing the first few characters of the line in question, killing the line(s) (C-k), jumping to the end of the buffer (M->) and yanking the killed line (C-y). Is there an easier way to do this?

Hamforrd answered 28/2, 2013 at 5:52 Comment(0)
M
12

You can use M-p and M-n to navigate up and down in the input history. Also, the current input can be used as a search pattern, i.e. type the start of the command you want to match, then M-p will take you to the next match. This uses the functions nrepl-previous-input and nrepl-next-input. If you don't like those keybindings, you can also rebind to <up> and <down>:

(define-key nrepl-mode-map (kbd "<up>") 'nrepl-previous-input)
(define-key nrepl-mode-map (kbd "<down>") 'nrepl-next-input)

Just add this to your .emacs (and evaluate C-x C-e after each line if you don't want to restart your Emacs). Also, note that M-n and M-p are likely to be bound to similar functionality in other REPL and comint like modes.

Molotov answered 28/2, 2013 at 6:51 Comment(0)
V
4

If you're using Cider, you can add the following to your user config:

(define-key cider-repl-mode-map (kbd "<up>") 'cider-repl-previous-input)
(define-key cider-repl-mode-map (kbd "<down>") 'cider-repl-next-input)

To persist the history for the next time you open a repl, you also have the following options:

(setq cider-repl-wrap-history t)
(setq cider-repl-history-size 1000)
(setq cider-repl-history-file "~/.cider-repl-history")

cider-repl-history-file is required if you want a persistent history. If you use a relative path, the history will be local to the current project.

Verminous answered 13/11, 2016 at 4:43 Comment(0)
M
0

The REPL History Browser section of Cider documentation describes the cider-repl-history command. Running that command will list previous REPL commands in a special-purpose cider-repl-history-mode mode, where you can move and choose commands.

While in the cider-repl-history-mode, you could run cider-repl-history-occur which asks for a regexp and then limits the command history output to only those that match the regexp, see here.

Arguably, the UX for this could be better- there should be a command that allows searching the history directly and without opening any new Emacs buffers, similar to cmd-R in Bash (reverse-i-search).

Monsoon answered 26/11, 2023 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.