5

Introducing My First Vim/Neovim Plugin

 3 years ago
source link: https://jdhao.github.io/2020/12/18/my_first_vim_plugin/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introducing My First Vim/Neovim Plugin

2020-12-18306 words 2 mins read 1 times read

After using and learning Neovim for over 2 years, I have finally published my first plugin – better-escape.vim.

Better-escape.vim is a plugin to help users escape from insert mode quickly without experiencing the lagging that comes with Vim mappings.

As a Vim user, a very popular mapping to escape insert mode is to map jj or jk or kj etc. to Esc, since the Esc key is hard to reach without moving your hand from the home row.

inoremap jk <ESC>
inoremap jj <ESC>
inoremap kj <ESC>

However, this comes with an annoyance. If you use jk to escape insert mode, whenever you press j in insert mode, Vim will not write it to the buffer, it will wait for timeoutlen milliseconds to see whether you want to use the jk mapping or you want to input j literally. This issue is easily noticeable when we have a relatively large timeoutlen value.

In my previous post, I have already talked about this topic and provided a solution. The solution is to use an insert mode mapping for k only. When we press k, we check if the previous character is j. If that is true, we will erase previous j and go to normal mode. Otherwise, k is inserted as is. There is no lagging any more. The problem is that now we can not input jk directly in insert mode. To insert k when the previous character is j, we need to press Ctrl-V, then press j.

In better-escape.vim, I have a smarter solution. We will check the time spent between pressing j and k in insert mode. If the time is above a predefined threshold, jk will be inserted literally. Otherwise, we will escape from insert mode. With this plugin, you can easily customize the shortcut for escaping insert mode, as well as the time threshold used.

Author jdhao

LastMod 2020-12-19

License CC BY-NC-ND 4.0

Reward
Next

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK