Combo keys: How implement it?


#1

Hi guys, one of my favorites extensions in vim/emacs are combo keys and key combination (leader key) for instance, I can press AC at the same time and compile,AD for debugging…this is pretty useful

other useful feature is key combination like “\ a c” using \ as leader key I don’t need press the 3 keys at the same time, I can do in sequential way and execute x command

I’m interested in get similar feature in atom, there are 2 possible options

  1. listen for every key stroke, if 2 keystrokes are pressed at the same time (with small delay between press) then remove the printed characters in the editor (AD) and execute the command…

  2. remove the event keypress from the editor and build a plugin which handle when 2 keys are pressed or not…if the user only press a key then execute the method in the original atom event listener

I dont like the option 1 because for sequential keys (similar to leader in vim) these would print all the keys pressed in the editor over several milliseconds…seems a bit nasty…

the second option seems more elegant but atom is using lambas for declare the event handlrs, so it’s hard I can remove them

WindowEventHandler.prototype.onKeydown = function(event) {
  atom.keymaps.handleKeyboardEvent(event);
  return event.stopImmediatePropagation();
};

do you know an alternative?..anyone knows how works vim atom?..thanks!!


#2

Have you checked out the vim-mode repo?


#3

Just to add to lee’s answer… They definitely do this in here in case curious. It uses a hyphen between keys if I remember correclty…See like ‘gg’ for an example I think.


#4

One more general strategy for package dev you probably already know but…

cd into your packages that you have downloaded, or particularly your /dev whichever set…

run ag (silver searcher) or some kind of grep across all of the packages for keywords, very useful. May or may not have found you this in this case, but very useful, I don’t see that mentioned often on the board…

I learned this with sublime package dev, but it took me a while, before realizing it…I think it should almost be added to the intro to packages page :smile:


#5

@leedohm yes I know the package vim mode, only I was trying found a more direct answer/solution, avoiding need check thousand lines of code from the vim package writes in coffeescript which personally I don’t like it…

thanks @DavidLGoldberg I did’t know about silver searcher, I was using ack but ag is a lot faster, thanks…


#6

Also, it’s output is super friendly, and has a few cool options too!


#7

I do this with spaces in the keymap:

  'ctrl-. s o': 'sort-lines:case-insensitive-sort'

That lets me type “CTRL-.” as my “leader” then “s” then “o” as a pnemonic for “sort”. I tested “\ s o” (I think you need blackslash twice to escape it), and it works fine.


#8

If anyone else came to this trying to figure out how to add leader-like keybindings, here’s what I do for my saves:

'atom-text-editor.vim-mode.command-mode':
    '\\ w': 'core:save'

Lets me tap the backslash key above the enter key, followed by the letter w to save the current file I’m on. Set to only save files when I’m in command mode, so not visual mode nor insert mode.