Cam Saul
Cam Saul

Reputation: 1737

Is there a way to do a history search in nrepl?

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?

Upvotes: 6

Views: 1789

Answers (3)

anon2328
anon2328

Reputation: 301

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

Upvotes: 0

apostl3pol
apostl3pol

Reputation: 984

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.

Upvotes: 4

danlei
danlei

Reputation: 14291

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.

Upvotes: 12

Related Questions