Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

remapping Esc #334

Closed
Closed

Description

@assemblical

I remapped 'jk' and 'kj' to activate command mode.

'.editor.vim-mode:not(.command-mode)':
  'j k': 'vim-mode:activate-command-mode'
  'k j': 'vim-mode:activate-command-mode'

However if I just press 'k' or 'j' followed by another letter, the 'k' or 'j' doesn't appear. How can I overcome this problem?

Activity

  1. just-be-dev commented on Jul 16, 2014

    @just-be-dev

    I've seen this as well. I usually map fj to escape in vim, however doing so in vim mode doesn't give the expected behavior. The first time 'f' is pressed, it always interprets it as a shortcut and waits on the rest to be entered. If something else is entered that isn't 'j' in this case then it just prints that out and continues on its marry way. In order to enter 'f' normally in my case I have to type it twice.

    The vim style behavior is to have a short timeout in which, if the shortcut isn't completed the typed letters are printed. I have the feeling that this is more of an atom issue than a plugin issue though so I fear it'll be hard to change.

  2. garcg commented on Jul 24, 2014

    @garcg
    Contributor

    It seems the timeout after the first key press is there, it only needs a fallback to insert the text into the editor.

    I've found a workaround adding the following to the user config.
    This is for mapping j k as escape:

    ~/.atom/keymap.cson:

    '.editor.vim-mode:not(.command-mode)':
      'j k': 'vim-mode:activate-command-mode'
    
    '.editor.vim-mode.insert-mode':
      'j': 'insert-incomplete-keybinding'

    ~/.atom/init.coffee:

    atom.workspaceView.command 'insert-incomplete-keybinding', (e)->
      if oe = e.originalEvent && e.originalEvent.originalEvent
        char = String.fromCharCode(oe.which)
        char = char.toLowerCase() unless oe.shift
        atom.workspace.activePaneItem.insertText(char)

    Although, I don't know how this could be fixed in the vim package.

  3. just-be-dev commented on Jul 28, 2014

    @just-be-dev

    It's good to know that it's doable though. I might explore a bit and see if I can come up with a solution.

  4. philk commented on Sep 2, 2014

    @philk

    This completely worked for me when I thought based on #129 that it was still impossible in Atom. Only caveat was hitting jj was canceling visual mode, not just insert mode, but that was an easy fix.

    '.editor.vim-mode.insert-mode':
      'j': 'insert-incomplete-keybinding'
    
    '.vim-mode.insert-mode:not(.mini)':
      'j j': 'vim-mode:activate-command-mode'

    Thanks, now I can use Atom without having to change my habits 😃

  5. BallisticPain commented on Jan 8, 2015

    @BallisticPain

    I have been unable to get jj to work for typing "j" and also using "jj" to get me out of command mode. :(

    I feel this shouldn't be so difficult. I have seen people reference a "timeout", but have been unable to find this anywhere in the settings for either Vim Mode nor Atom.

    Edit: I just re-checked the init.coffee and noticed a misspelling... works now! Woohoo...

    Thanks!
    J

  6. pelachile commented on Jan 10, 2015

    @pelachile

    @BallisticPain where did you find the misspelling? I have tried and tried and I can't get jj to work. It's the only thing that prevents me from using atom, I'm just to used to my vim settings. I'm on a OSX 10.10

  7. BallisticPain commented on Jan 12, 2015

    @BallisticPain

    @pelachile, I'll copy and paste below what I have that's working. I use jj constantly and it was imperative that I got it working as well so I fully understand your frustration.

    I personally mis-spelled a portion of the ~/.atom/init.coffee script. I didn't copy and paste, I just typed it in.

    # Your init script
    #
    # Atom will evaluate this file each time a new window is opened. It is run
    # after packages are loaded/activated and after the previous editor state
    # has been restored.
    #
    # An example hack to make opened Markdown files always be soft wrapped:
    #
    # path = require 'path'
    #
    # atom.workspaceView.eachEditorView (editorView) ->
    #   editor = editorView.getEditor()
    #   if path.extname(editor.getPath()) is '.md'
    #     editor.setSoftWrap(true)
    #
    
    atom.workspaceView.command 'insert-incomplete-keybinding', (e)->
        if oe = e.originalEvent && e.originalEvent.originalEvent
            char = String.fromCharCode(oe.which)
            char = char.toLowerCase() unless oe.shift
            atom.workspace.activePaneItem.insertText(char)

    I misspelled the .workspace portion.

    Here's my ~/.atom/keymap.cson (I was originally modifying the one in the package directly, and this wasn't working as well as modifying my home directory version)...

    # Your keymap
    #
    # Atom keymaps work similarly to stylesheets. Just as stylesheets use selectors
    # to apply styles to elements, Atom keymaps use selectors to associate
    # keystrokes with events in specific contexts.
    #
    # You can create a new keybinding in this file by typing "key" and then hitting
    # tab.
    #
    # Here's an example taken from Atom's built-in keymap:
    #
    # '.editor':
    #   'enter': 'editor:newline'
    #
    # '.workspace':
    #   'ctrl-P': 'core:move-up'
    #   'ctrl-p': 'core:move-down'
    #
    '.editor.vim-mode.insert-mode':
        'j': 'insert-incomplete-keybinding'
    
    '.vim-mode.insert-mode:not(.mini)':
      'j j': 'vim-mode:activate-command-mode'

    One thing I will also note here is that I believe the order of these two insertions matters. The other note is that if you want to type a 'j' while in insert mode, type it and then the next letter and they'll both appear. Otherwise you must wait for the timeout to occur to see the 'j' appear.

    I hope this helps,
    Jarvis

  8. JacobHTran commented on Feb 18, 2015

    @JacobHTran

    Atom seems to have a new process to add custom commands. I have been playing with atom for all of 10 mins, but in order to achieve this functionality without using the deprecated atom.workspaceView.command I implement the insert-incomplete-keybinding as such:

    atom.commands.add 'atom-text-editor', 'insert-incomplete-keybinding', (e)->
      oe = e.originalEvent
      char = String.fromCharCode(oe.which)
      char = char.toLowerCase() unless oe.shift
      atom.workspace.activePaneItem.insertText(char)
  9. rdlugosz commented on Mar 24, 2015

    @rdlugosz

    I was still seeing odd delays with the methods described above and so I implemented things in a slightly different way. Note that I prefer to use kj to exit insert mode rather than jk or jj, but you should be able to modify this for your own approach.

    #keymap.cson
    'atom-text-editor.vim-mode.insert-mode':
      'j': 'exit-insert-mode-if-preceded-by-k'
    #init.coffee
    atom.commands.add 'atom-text-editor', 'exit-insert-mode-if-preceded-by-k': (e) ->
      editor = @getModel()
      pos = editor.getCursorBufferPosition()
      range = [pos.traverse([0,-1]), pos]
      lastChar = editor.getTextInBufferRange(range)
      if lastChar != "k"
        e.abortKeyBinding()
      else
        editor.backspace()
        atom.commands.dispatch(e.currentTarget, 'vim-mode:activate-normal-mode')

    While this isn't quite as flexible (in terms of being able to map different keys without changing the command that's executed), there are no "timeout" related issues or visual delays in not having the j (or in my case, k) char appear on the screen until the next keypress. This also avoids issues related to Atom "eating" the first char of your mapping, which can happen with other approaches.

    Note that using the event.abortKeyBinding() is the preferred way to jump out of your key bindings and let the keystroke pass through to the editor (see: https://atom.io/docs/api/v0.187.0/KeymapManager).

    edit: the word should have been "preceded" not "proceeded".
    edit2: changed .editor to atom-text-editor to resolve Atom 1.0 deprecation warning
    edit3: 22July2015 - Changed vim-mode:activate-command-mode to vim-mode:activate-normal-mode for compatibility with v.56+

  10. Robert-Wett commented on Mar 25, 2015

    @Robert-Wett

    +1 @rdlugosz - working great for me, using jk to exit insert mode. For the lazy:

    #keymap.cson
    'atom-text-editor.vim-mode.insert-mode':
      'k': 'exit-insert-mode-if-proceeded-by-j'
    #init.coffee
    atom.commands.add 'atom-text-editor', 'exit-insert-mode-if-proceeded-by-j': (e) ->
      editor = @getModel()
      pos = editor.getCursorBufferPosition()
      range = [pos.traverse([0,-1]), pos]
      lastChar = editor.getTextInBufferRange(range)
      if lastChar != "j"
        e.abortKeyBinding()
      else
        editor.backspace()
        atom.commands.dispatch(e.currentTarget, 'vim-mode:activate-normal-mode')
  11. jesseleite commented on May 6, 2015

    @jesseleite

    It should be as easy as:

    'atom-text-editor.vim-mode':
      'j k': 'vim-mode:reset-command-mode'

    Unfortunately, it's not. Small things like this keep me using Sublime. I'm excited about Atom's future though.

  12. ahmadseleem commented on May 14, 2015

    @ahmadseleem

    The best and simplest until this is supported natively by Atom:
    Is the one by: @Robert-Wett
    because the the one by @garcg doesn't work if yo wanna map escape to something like =>

     , ,
    

    So, the final solution is as @Robert-Wett except he didn't add escape for visual mode in the mapping:

    #keymap.cson
    
    #................................................
    # Escape
    #................................................
    'atom-text-editor.editor.vim-mode.insert-mode':
      ',': 'exit-insert-mode-if-proceeded-by-j'
    
    'atom-text-editor.vim-mode.visual-mode:not(.mini)':
      ', ,': 'vim-mode:activate-command-mode'
      'v': 'vim-mode:activate-command-mode'

    It's still not perfect because it just wait for the second letter for ever....!!!
    it should be just a half a second or so then inserts the char and return...

  13. ahmadseleem commented on May 14, 2015

    @ahmadseleem

    Like:

     setTimeout(doStuff, 50); //wait 50 millisecnds then recheck
     return;

    I know it's extremely rare that you would need to type jj or jk or ,, or whatever alike.
    But, just incase

  14. bronson commented on May 21, 2015

    @bronson
    Contributor

    Is this a dupe of #221?

  15. 84 remaining items

  16. martomes commented on Feb 18, 2016

    @martomes

    @rdlugosz No, i mean this snippet:

    'atom-text-editor.vim-mode.insert-mode':
      'j k': 'vim-mode:activate-normal-mode'

    But actually non of the snippets posted in this thread is working for me. Solutions with init script functions, solutions without it... no chance to make it operational.

    I have tried reinstallation, disabling the other plugins etc . without any success. An I dunno how to debug it.

  17. martomes commented on Feb 18, 2016

    @martomes

    @rdlugosz well... only solution which works for me is upgrade to beta version (1.6.0-beta4)

  18. kylesezhi commented on Feb 19, 2016

    @kylesezhi

    I have 1.5.3 and this keymap works for me:

    'atom-text-editor.vim-mode.insert-mode':
       'j k': 'vim-mode:activate-normal-mode'

    Note that I have a linebreak after the first colon and the second line is tabbed in.

  19. Yohanna commented on Mar 17, 2016

    @Yohanna

    @kylesezhi Your kemap worked for me, thanks!

    For the record, I'm using v. 1.5.4

  20. jesseleite commented on Mar 17, 2016

    @jesseleite

    @kylesezhi and @Yohanna that keymap always worked. The issue is with the delay when typing anything like "jam" or "jesse". There's a bad delay after hitting that j key while the keymap listener is waiting for a possible k to follow.

    Other vim emulators do their check after hitting the second k input. The emulator then checks the previous key to see if it was a j, and if so enters vim mode. This allows the vim emulator to handle the same j k key mapping without that delay.

  21. rdlugosz commented on Mar 17, 2016

    @rdlugosz

    Exactly. Avoiding the delay is precisely what my post above (and PR to have
    this added to the Readme) is all about.

    One step further that i think is worth considering is to just add the code
    to the package itself and allow the user to specify their preferred two-key
    mapping to exit insert mode via the vim-mode config screens. It's debatable
    as to whether something like this belongs in the config gui, but mapping a
    sequence like this is such a common thing that it could be justified.
    (Assuming, of course, adding this to the config would allow users to skip
    the business of adding the custom function to the init script.)

    To be perfectly honest I'm not a daily Atom user so can't justify looking
    into building this right now. When I do use Atom I'd like the mappings to
    work properly, which is what my approach allows.

    On Thursday, March 17, 2016, Jesse Leite [email protected] wrote:

    @kylesezhi https://github.com/kylesezhi and @Yohanna
    https://github.com/Yohanna that keymap always worked. The issue is with
    the delay when typing anything like "jam" or "jesse". There's a bad delay
    after hitting that j key while the keymap listener is waiting for a
    possible k to follow.

    Other vim emulators do their check when you hit the second k input. When k
    is hit, the emulator checks the previous key to see if it was a j, and if
    so enters vim mode. This allows the vim emulator to handle the same key
    mapping without delay.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #334 (comment)

  22. kylesezhi commented on Mar 18, 2016

    @kylesezhi

    Thanks for that explanation, @jesseleite !

    I agree - checking after the k in jk would be an improvement on the current way the system works. I'd love to see this in a future build of Atom.

  23. jesseleite commented on Mar 18, 2016

    @jesseleite

    Until then, you can add @rdlugosz's solution above to your init script and keymap :)

  24. denysdovhan commented on May 6, 2016

    @denysdovhan

    Yep, but it still doesn't work well for non-american keyboards.

  25. Khangeldy commented on May 16, 2016

    @Khangeldy

    After remap to k j . It is hard to import sockjs

  26. mugukamil commented on Jun 25, 2016

    @mugukamil

    @denysdovhan Yes it is... :(

  27. rdlugosz commented on Oct 5, 2016

    @rdlugosz

    While that's true, it's also true in Vim proper. Probably why the "official" way to leave insert mode is ESC instead of some combination of letters you may want to type. :)

    Simple enough to add one additional check to the code to see if you might be typing something like 'sockjs' (look for a 'c' before the 'k') if this hits you a lot.

    I do have a PR open (#946) to try and get some mention of this in the Readme. Anyone interested in reviewing it and merging and/or otherwise getting it documented? AFAIK, this is still the "best" method of remapping ESC in Atom.

  28. glrodasz commented on Oct 13, 2016

    @glrodasz

    @denysdovhan Why it is that? I have an non-american keyboard and works well.

  29. denysdovhan commented on Oct 14, 2016

    @denysdovhan

    @garethderioth I have no idea why. Now, I just use vim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions