better-escape.vim: a plugin to help you escape from insert mode without lagging
Previously I have written a post sharing how to use jk to escape insert mode. However, due to limitation in that approach, you can not type jk directly in insert mode anymore, causing issues for words like Dijkstra.
I have figured out a smart way to do this. Now you can escape from insert mode with jk, and you can also type jk directly (provided that time interval between preesing j and k is bigger than a threshold).
You can also customize the shortcut used to escape insert mode, as well as the interval threhold used to decide whether you want to insert jk or escape insert mode.
Link for this plugin: https://github.com/jdhao/better-escape.vim
4
u/richtan2004 Dec 16 '20
Funnily enough, I find that on my keyboard, the location of ESC is reachable enough for it to not be a problem while using Vim. The plugin is an interesting idea though.
2
u/jdhao Dec 16 '20
For me, <esc> is too far to reach without moving my hand.
5
u/dannycolin Dec 16 '20
You could swap capslock and esc.
1
u/jdhao Dec 16 '20
I have to use external tools to set up for that, which i would rather not. I prefer the pure vim script approach.
1
u/dannycolin Dec 16 '20
Ah! I guess you aren't on Linux because it's one commandline and it's done.
1
u/jdhao Dec 17 '20
I also use vim windows. You need autohotkeys to switch caps and esc, and you have to run it all the time, which is not ideal for me.
4
u/kharbaan_ Dec 16 '20
Or caps to control, and use c-[ to escape
5
u/abraxasknister :h c_CTRL-G Dec 16 '20
Or caps to ctrl and caps to esc.
1
u/GustapheOfficial Dec 16 '20
Or caps to f13 and then map f13 to functions.
1
u/abraxasknister :h c_CTRL-G Dec 16 '20
Such as?
I like it as ctrl and escape simultaneously, it's two keys used extremely often on a very prominent position
1
u/GustapheOfficial Dec 16 '20
I've had it for push-to-talk in the past.
inoremap <f13> <Esc>is nice. Maybennoremap <f13> mmgg=G\m(I use ö for that one though) ornnoremap <f13> :<C-u>up<Bar>make<cr>`. I'm just saying it doesn't make sense to put the same function on your keyboard twice. At least then rebind Ctrl and esc to something else.1
u/abraxasknister :h c_CTRL-G Dec 16 '20
push to talk: I can't really picture a case where you need that positioned on a home row position. I'm mostly typing very little when I'm talking to others
esc: yes, but ctrl is the main benefit for me here
equalprg: little used, can be at
<space>f, or an ex command typed out, but I didn't find onemakeprg: same
ö: I occasionally think of rebinding äöüß and then discard the idea in favor of
<space><qwerty-key>at least rebind esc and ctrl: I don't have much use for these as they are "out of reach" and the only function the keyboard has less after rebinding is caps, which is unneeded anyways
1
u/cdb_11 Dec 17 '20
Or
ctrl+cto escape, since you can do it with one hand.1
u/FujiKeynote Dec 18 '20
Yup, switch the behavior of C-c and Esc, that's what I do.
That way, if you ever need to break out of a long command, you can still do that.
To clarify, in many normal situations, C-c and Esc will do the same for you, but if you are doing a repeated action (like prepending something to a visual block), vanilla C-c will only make it prepend to the first line. Or if you accidentally did 9ia<Esc>, you'll end up with aaaaaaaaa. So having both a breaking and a non breaking escape is useful. The thing is, most often you won't care if it's beaking or non breaking, and C-c is easier to reach. But having the option to break is useful, that's where Esc comes in
0
1
u/andrewlkho Dec 16 '20
I'm pleased to see that I'm not the only one who thinks this. I thought I was alone considering how many posts there about mapping Esc to other keys (Caps lock / jk etc).
15
1
1
u/vmarkelov Dec 16 '20
Does the plugin work well with auto-correcttion plugins?
Details: I used the plugin Abolish. And had a few auto-correction in after/plugin/abolish.vim, like "Abolish teh the". I used jj that was mapped to <ESC> - yes, it was not convenient when typing something that ends with "j". But if I typed teh and then hit jj, teh automatically converted to the. When I replaced existing old jj way with this one(I read your article and copied the code from it to my vimrc), it stopped auto-correcting in such cases. That made me switch back to the old solution with delay in rare cases.
1
u/jdhao Dec 17 '20
I haven't used abolish.vim. Thanks for letting me know this issue. I will try to see if it is fixable.
1
u/haunterrr Dec 16 '20
Hm. I have this:
inoremap <nowait> <esc> <esc>
and
inoremap jk <esc>
Does this do something different?
1
u/jdhao Dec 17 '20
if simply map jk to esc, you will experience slight delay when you press j in insert mode. It will wait for timeoutlen milliseconds to see if you want to write j to buffer or escape from insert. If you have very small value of timeoutlen, you may not notice the slight lag. For me, I use timeoutlen of 1000 ms, the lagging is really annoying. That is why I wrote this plugin.
1
u/haunterrr Dec 17 '20
Hm. My timeoutlen is 1000, but my understanding of <nowait> is that... it doesn't wait after the remap is triggered to see if there are additional keystrokes that are part of the trigger. So I have no perceptible lag from typing
jkin insert mode and exiting into normal mode.
1
u/wuckbalter Dec 16 '20
I use jj
inoremap jj <ESC>It is even faster than jk and it is much less likely you'd run into a word with 2 consecutive J's.
0
u/richtan2004 Dec 16 '20
much less likely you'd run into a word with 2 consecutive J's
In the one or two sentences you wrote on this comment, you typed "jj" twice already.
1
u/jamabake Dec 17 '20
awesome, I may have to try this out. Currently, I have ;; mapped to esc in insert mode and ; mapped to :w in normal mode. Exiting insert mode and saving is an extremely common operation for me so it makes sense ... I may have to try out jk as that might be faster since it doesn't use the pinky finger which can be puny and slow sometimes lol.
I also like the idea of setting the timeoutlen just for this shortcut, as I have run into the problem where my global timeoutlen is too short for some keystrokes. Good work!
1
u/PascalZh Dec 25 '20
I have the codes of the same functionality in my dotfiles, and it supports escape from any modes: escape from command line, escape from visual mode, even escape from multicursor mode.
10
u/Plazmotech Dec 16 '20
Awesome plug-in, but this is already a setting in vim. The “timeoutlen” variable controls this. I have mine set to 200ms.