# DrRacket REPL: Make ctrl-p an alias for ctrl-up

**URL:** <https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882>\
**Category:** General\
**Tags:** drracket, repl, keyboard\
**Created:** [April 20, 2023, 6:41pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882 "2023-04-20T18:41:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 6:41pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/1 "2023-04-20T18:41:34Z")

</div>

In the DrRacket REPL (interaction window), I can use ctrl-up (up = arrow up) to recall one of the previous inputs. However, recently I've become used to use ctrl-p in Bash to recall previous inputs, because that's easier to type if you touch type. (It's not necessary to reach for the arrow key block.)

The problem is that if I habitually press ctrl-p in the DrRacket interaction window, I get a print dialog, which is somewhat annoying. Since I practically never need printing in DrRacket, I'd like to change the ctrl-p keybinding to be equivalent to ctrl-up. Is this possible, and if yes, how?

I found the "Keybindings" option in the "Edit" dropdown menu and the documentation on DrRacket [3.3&nbsp;Keyboard Shortcuts](https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html#%28part._defining-shortcuts%29) , but I don't understand how to apply the information there to my problem.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [April 23, 2023, 8:10pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/2 "2023-04-23T20:10:37Z")

</div>

Quickly: It appears the keymap function name is `put-previous-sexp`.  
To call this method, you need to find the interaction's keymap, then use the keymap API to call by name. I may be able to dig a little more tomorrow.

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 23, 2023, 9:29pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/3 "2023-04-23T21:29:28Z")

</div>

> [@Laurent.O](#):
>
> I may be able to dig a little more tomorrow.

That would be great, but it doesn't have to be right tomorrow if that's difficult for you. 🙂

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [April 24, 2023, 6:45am UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/4 "2023-04-24T06:45:12Z")

</div>

Quickly again, here's a quickscript that does the job:

```plaintext
#lang racket/base

(require quickscript
         racket/class
         racket/gui/event)

;; Scratchpad to try scripts.

;; Returns a replacement string for the selected string `selection`
;; ("" if no text is selected), or `#f` to leave the selection as is.
(define-script find-keymap
  #:label "keymap"
  #:menu-path ("Scratch")
  (λ (selection #:interactions ints)
    (define km (send ints get-keymap))
    (send km call-function "put-previous-sexp" ints (new event%) #t)
    #f))

```

I guess you probably want just a keybinding. Unfortunately I was not able to overload the predefined Ctrl-p. (More and more I believe the whole keymap business needs to be rethought, but that's quite some work.)

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 24, 2023, 12:47pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/5 "2023-04-24T12:47:37Z")

</div>

> [@Laurent.O](#):
>
> Quickly again, here's a quickscript that does the job:

Thanks for the script.

I've never worked with quickscripts before. It seems I was able to activate it, as I get an entry `Scratch/keymap` in DrRacket's `Script` menu. Selecting `keymap` actually recalls the history, but ...

> [@Laurent.O](#):
>
> I guess you probably want just a keybinding.

yes, that's the idea. 🙂 If I have to select a menu item to go through the history, I might as well use ctrl-up as before.

> [@Laurent.O](#):
>
> Unfortunately I was not able to overload the predefined Ctrl-p. (More and more I believe the whole keymap business needs to be rethought, but that's quite some work.)

Ok, that it (probably) doesn't work is also useful information (in a way).

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [April 24, 2023, 2:02pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/6 "2023-04-24T14:02:40Z")

</div>

I haven't tried yet, but maybe via a keybinding it's possible to overload ctl-p.

---

<div class="post-metadata">

**Author:** ![jryans](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jryans/32/31_2.png) [@jryans](https://racket.discourse.group/u/jryans)\
**Post date:** [April 24, 2023, 2:24pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/7 "2023-04-24T14:24:55Z")

</div>

You should be able to use a custom keybindings file to rebind Ctrl-P via the method described on the shortcuts page. Here's an example file which worked for me:

```scheme
#lang s-exp framework/keybinding-lang

(define (rebind key command)
  (keybinding
   key
   (λ (ed evt)
     (send (send ed get-keymap) call-function
           command ed evt #t))))

(rebind "c:p" "put-previous-sexp")

```

The `rebind` definition above is the same as the one given on the documentation page.

It does take a bit of trial and error to work out what's going on when customising keys in this way... I suspect the documentation and / or DrRacket could be improved to make it easier to achieve quick customisations like this.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [April 24, 2023, 4:36pm UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/8 "2023-04-24T16:36:06Z")

</div>

Haven't tested on Windows, but does esc-p work out of the box?

[Esc followed by p -- not esc and p simultaneously.]

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [April 25, 2023, 10:14am UTC](https://racket.discourse.group/t/drracket-repl-make-ctrl-p-an-alias-for-ctrl-up/1882/9 "2023-04-25T10:14:10Z")

</div>

[Esc] followed by [p] works for me on Windows and macOS, and is documented/expected behaviour: [1.6&nbsp;The Interactions Window](https://docs.racket-lang.org/drracket/interactions-window.html)

keybindings has the following for `put-previous-sexp`  
On windows

- m:~g:p
- esc;p
- c:up

on macOS

- esc;p
- c:up

where for me `c` is the control key on macOS and Windows, and `m` is the Alt key to the left of the spacebar on my windows laptop.

In the above notation;

- `:` indicates the subsequent is pressed while the former is held down
- `;` indicates a sequence of key presses

**Does anyone know what keying behaviour `~` indicates?**

Fun fact I did not realise - the keybindings window changes what it shows you depending if you have the cursor in definitions or interactions.

Interactions left

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/c/c9eaf5994763b2ab8b310c59dd7144df8f6008a8.jpeg) ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/d/d6bb63beec1024223f588f25c1a4800047a377db.jpeg)  
definitions right

The page [3.3&nbsp;Keyboard Shortcuts](https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html) uses a different notation for when 'enable keybindings in menus' is unticked:

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/6/6a60f5333e12c3fb60829d23a44e8f841039a9b3.png)

Capitalised 'C' for control, 'M' for meta, ESC, etc.  
I believe

- `-` indicates the subsequent is pressed while the former is held down
- ` `(space) indicates a sequence of key presses

The keybindings window changes - no 'Print Definitions' keybinding when definitions is selected?

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/a/afa26c49fe65c4a6c0f82e23cf7e437504b5758b.jpeg)

Sorry for taking you with me on this journey down the keybinding rabbit hole.

Stephen :beetle:
