Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remap escape insert mode to 'jk' in fish shell?

Tags:

shell

vim

vi

fish

I use jk as my escape sequence in vim as well as vi-mode for bash and zshell. How do I do this in fish shell?

i.e. in vim:

inoremap jk <esc>

bash:

bind -m vi-insert '"jk": vi-movement-mode'

zsh

bindkey -M viins 'jk' vi-cmd-mode

What is the fish shell equivalent?

like image 572
willpnw Avatar asked Feb 23 '18 22:02

willpnw


2 Answers

I'm assuming you've already enabled vi mode by executing fish_vi_key_bindings. Otherwise the question doesn't make sense :-)

Create a file named ~/.config/fish/functions/fish_user_key_bindings.fish that contains this:

function fish_user_key_bindings
    bind -M insert jk "if commandline -P; commandline -f cancel; else; set fish_bind_mode default; commandline -f backward-char force-repaint; end"
end

You can run the bind interactively but it won't be persistent across new fish sessions unless you create that autoloaded function. Also, if you switch between vi and emacs binding you'll want to guard that bind so it's only done for vi mode:

if test "$__fish_active_key_bindings" = "fish_vi_key_bindings"
    bind ....
end
like image 76
Kurtis Rader Avatar answered Oct 30 '22 09:10

Kurtis Rader


Here is a blog post that does exactly what I was looking for:

https://fedragon.github.io/blog/2015/03/22/vimode-fishshell-osx/

TLDR;)

~/.config/fish/functions/fish_user_key_bindings.fish
---------------------------------------------------
function fish_user_key_bindings
  fish_vi_key_bindings
  bind -M insert -m default jk backward-char force-repaint
end

And

~/.config/fish/config.fish
---------------------------------------------------
set fish_key_bindings fish_user_key_bindings
like image 21
willpnw Avatar answered Oct 30 '22 09:10

willpnw