# Overriding (some?) key bindings in REPL does not seem to work

**URL:** <https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830>\
**Category:** General Usage\
**Tags:** repl, startup\
**Created:** [September 6, 2022, 6:46am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830 "2022-09-06T06:46:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 6, 2022, 6:46am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/1 "2022-09-06T06:46:41Z")

</div>

Hello!

I’m trying to override a default keybinding in REPL, as [suggested in the manual](https://docs.julialang.org/en/v1/stdlib/REPL/#Customizing-keybindings), with the following code in `startup.jl`:

```nohighlight
import REPL
import REPL.LineEdit

atreplinit() do repl
    my_keys = Dict("^w" => (s,o...) -> LineEdit.edit_kill_region(s))
    repl.interface = REPL.setup_interface(repl; extra_repl_keymap = my_keys)
end

```

After Julia REPL startup, however, `C-w` still performs its default operation. The example in the manual shows how to override up and down arrow behavior, so I’m assuming that overriding _is_ possible, but I can’t seem to get it working. Any ideas what could have I done wrong?

When I bind a still unused key, that seems to work just fine. In fact, it works even for single key shortcuts, which would otherwise paste themselves in the input buffer (e.g., the key `w`).

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 6, 2022, 10:36am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/2 "2022-09-06T10:36:03Z")

</div>

Your example works for me, I am on Windows 10 using Windows Terminal.  
To see a more clear effect I modified your code to do a clear and this works too:

```julia
import REPL
import REPL.LineEdit
import REPL.Terminals

atreplinit() do repl
	mykeys = Dict(
		"^w" => (s,o...)->(Terminals.clear(REPL.terminal(s)); LineEdit.refresh_line(s))
		#"^w" => (s,o...) -> LineEdit.edit_kill_region(s)
	)
	repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)
end

```

What’s your environment/OS/Terminal?

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 6, 2022, 11:08am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/3 "2022-09-06T11:08:28Z")

</div>

Debian 11, Gnome Terminal, zsh.

But I think I just realized what the crux of the problem was. For the sake of quicker REPL startups, I’m compiling my own system images with `PackageCompiler`, which include `OhMyREPL` as well. If I start REPL without the custom system image, then the keybinding works as documented and expected. With the precompiled system image, however, it does not. That’s odd, because `startup.jl` loads and runs just fine even with the system image.

I’m now trying to figure out how to embed my custom keybinding in the system image, but it isn’t trivial.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 6, 2022, 11:27am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/4 "2022-09-06T11:27:32Z")

</div>

For you system image you can just change the REPL src. In your case it would be in  
…\Julia-1.8.0\share\julia\stdlib\v1.8\REPL\src\LineEdit.jl  
From line 1982 (setup\_search\_keymap) and below.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 6, 2022, 11:29am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/5 "2022-09-06T11:29:50Z")

</div>

But perhaps there is a way to exclude REPL from the precompiled system image, but I don’t know how.

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 6, 2022, 11:38am UTC](https://discourse.julialang.org/t/overriding-some-key-bindings-in-repl-does-not-seem-to-work/86830/6 "2022-09-06T11:38:33Z")

</div>

I finally figured this out. It was actually a combination of `OhMyREPL` and the system image compilation that caused the issue. But the solution is much simpler than expected, even though it _is_ a bit fragile and could break with future releases of `OhMyREPL`.

This needs to go in the `startup.jl`, in the `atreplinit()` block:

```julia
        OhMyREPL.Prompt.NEW_KEYBINDINGS["^W"] = function (s,o...)
            LineEdit.edit_kill_region(s)
            OhMyREPL.Prompt.rewrite_with_ANSI(s)
        end

```

This is because `OhMyREPL` already replaces the default keybinding with its own, and we need to override _that_.
