question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow a key sequence binding like `j,k` to not prevent user from insert `j`

See original GitHub issue

Description of the new feature/enhancement

You can bind a key sequence like j,k to a function or script block by Set-PSReadLineKeyHandler -Key 'j,k' -ViMode Insert -Function ViCommandMode, but a user cannot insert a character j after that.

Proposed technical implementation details (optional)

Quoted from https://github.com/PowerShell/PSReadLine/issues/1701#issuecomment-664020465

In vim, jk or jj to exit insert mode is super common. Usually it works like this:

  1. You press <kbd>j</kbd>
  2. <kbd>j</kbd> is written to the buffer
  3. A timer starts
  4. If you press the next key in the sequence before the timer is up, the j is removed from the buffer and the command fires
  5. If the key you press next isn’t in the sequence, or the timer is up then it’s treated like a literal

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
Drllapcommented, Jan 23, 2022

This is what I added to my profile to make jk go to comand mode:

$j_timer = New-Object System.Diagnostics.Stopwatch

Set-PSReadLineKeyHandler -Key j -ViMode Insert -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert("j")
    $j_timer.Restart()
}

Set-PSReadLineKeyHandler -Key k -ViMode Insert -ScriptBlock {
    if (!$j_timer.IsRunning -or $j_timer.ElapsedMilliseconds -gt 1000) {
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert("k")
    } else {
        [Microsoft.PowerShell.PSConsoleReadLine]::ViCommandMode()
        $line = $null
        $cursor = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
        [Microsoft.PowerShell.PSConsoleReadLine]::Delete($cursor, 1)
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor-1)
    }
}
6reactions
SeeminglySciencecommented, Jul 26, 2020

Yeah, j will be bound after that and won’t be treated as a literal character. I think that’s just an example.

Nah jk or jj (what I use) to exit insert mode is super common. Usually it works like this:

  1. You press <kbd>j</kbd>
  2. j is written to the buffer
  3. A timer starts
  4. If you press the next key in the sequence before the timer is up, the j is removed from the buffer and the command fires
  5. If the key you press next isn’t in the sequence, or the timer is up then it’s treated like a literal
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using 'jk' to exit insert mode with key-chord or anything else
A key cannot be bound to two different commands. In particular, it cannot be bound to both self-insert-command (or equivalent), which inserts a ......
Read more >
How to remap escape insert mode to 'jk' in fish shell?
I use jk as my escape sequence in vim as well as vi-mode for bash and zshell. How do I do this in...
Read more >
Avoid the escape key | Vim Tips Wiki - Fandom
A problem with mapping a sequence like jj is that Vim will pause whenever you type j in insert mode (it is waiting...
Read more >
Fish shell: Map jk to enter normal mode (in vi mode)
The key sequence of the binding is jk , and its command is to repaint. (The repainting is necessary if your prompt shows...
Read more >
VSCode Neovim
Neovim is a fork of VIM to allow greater extensibility and integration. ... the key sequence to activate the binding "key": "ctrl+h", //...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found