How to replace a keymap binding


#1

I would like to change the key binding for ctrl-j, so I add a mapping to the keymap

‘atom-text-editor’:
‘ctrl-j’: ‘core:move-left’

The problem is the previous keybind is still in effect so the previous one takes precedence. The Atom documentation says you can use ‘ctrl-j’: ‘unset!’. So I tried

‘editor’:
‘ctrl-j’: ‘unset!’

‘atom-text-editor’:
‘ctrl-j’: ‘core:move-left’

and

‘body’:
‘ctrl-j’: ‘unset!’

‘atom-text-editor’:
‘ctrl-j’: ‘core:move-left’

and

‘atom-text-editor’:
‘ctrl-j’: ‘unset!’
‘ctrl-j’: ‘core:move-left’

None of them worked. So what is the proper procedure for clearing out any previous key mapping and replacing it with a new one?


#2

You can open the keybinding resolver (ctrl+.) and then press the keybinding to see which scope it is bound to. Then you can unset! that key combo for that specific scope.


#3

In addition to @olmokramer’s advice, you can use the Keybinding tab in the Settings View (Cmd+, on OS X and Ctrl+, on Linux/Windows) to look up the offending key binding. Once there, you can use the clipboard icon to the left to copy the definition for pasting directly into your keymap.cson.


#4

So if the keybinding is:

'atom-workspace atom-text-editor:not([mini])':
  'ctrl-j': 'editor:join-lines'

I would add the following to my local key binding file:

'atom-workspace atom-text-editor:not([mini])':
  'ctrl-j': 'unset!'
'atom-text-editor':
  'ctrl-j': "custom:my-command'

This still exhibits the same issue - both commands are run instead of just one. The keybinding resolver indicates there are now three bindings on this keystroke: ‘editor:join-lines’, ‘custom:my-command’, and ‘unset!’, with ‘editor:join-lines’ and ‘unset!’ in the same scope of ‘atom-workspace atom-text-editor:not([mini])’


#5

If you can show a screenshot of the Keybinding Resolver, one of the commands should be a different color. That is the only one that should be executed.


#6

They are all the same color - gray.


#7

Looks like none of them are being executed? I’ll have to take a look at this a bit when I get home.


#8

It looks like the “editor:join-lines” is the only one being executed. If I change the third one to “core:move-up”, the cursor does not move up - the two lines are just joined.

Appreciate your help so far.


#9

So here’s what I ended up doing that worked:

'atom-workspace atom-text-editor:not([mini])':
  'ctrl-j': 'core:move-left'
'atom-text-editor':
  'ctrl-j': 'core:move-left'

Instead of unset!, I just went ahead and mapped core:move-left to Ctrl+J in the atom-workspace atom-text-editor:not([mini]) selector. And then to ensure that Ctrl+J worked in mini text editors too, I mapped general atom-text-editor as well.

Let me know if that helps :grinning:


What's wrong with my ctrl-[hjkl] and ctrl-\ keybindings?
#10

Not sure why the “unset!” doesn’t work like documented, but I’ll take your solution because it seems to work.


#11

There’s an Issue open on this here:


#12

Thanks!! PS. It works, but it requires to restart atom, not just saving the keymap.cson file as in other cases.


#13

There is a package called atom-save-all that after it is installed, all modified panels will save when ctrl-s is pressed.


#14

I’m struggling with this – why would a package override my keymap with identical CSS selectors? This is on atom 1.6.0.

I thought that my keymap would take precedence in this case. Sorry about the dim colors.


#15

It’s hard to answer that without knowing exactly what you have in your keymap.cson.


#16

Sorry, Here is the keymap.cson. I tried to match the selector in the encoding-selector module:

'.platform-linux atom-text-editor':
    'alt-u': 'git-diff-details:undo'

(this is the entirety of my keymap.cson).


#17

It looks like it should win, but I also don’t know much about the git-diff-details package. Does git-diff-details use an atom-text-editor element? Perhaps the command you’re trying to map doesn’t apply in that context?


#18

You’re right. I was testing in the tab that had keymap.cson open. Not a git repo, and I guess the git-diff-details package didn’t apply.

It works perfectly in a git repo. I was misunderstanding the key binding resolver.

Thanks for the help!


#19

Great! I’m glad this is working for you now :tada: :boom:


#20

Does overriding with atom-workspace work anymore?

I can’t get this keymap to work:

It always resolves to regular tab.