In the Atom editor, is it possible to define a key binding (key map) only for a certain file type?
E.g. so that the key binding only works when editing a Markdown file.
Here is a real-world example with a useful override for Markdown grammar files. It resolves a collision with the Emmet package that consumes Ctrl+Shift+M und blocks Markdown package from showing its preview pane. This key binding only becomes active for files that have been recognized (or manually set) to be Markdown grammar.
'atom-workspace atom-text-editor[data-grammar="source gfm"]':
'ctrl-shift-M': 'markdown-preview:toggle'
'atom-workspace atom-text-editor[data-grammar="text md"]':
'ctrl-shift-M': 'markdown-preview:toggle'
If you are unsure about which IDs to use for the grammar: Go to Atom's settings, click on "Packages" the navigation sidebar on the left, search for the language of your choice, select it, and look for the Grammar / Scope explanation. Use the ones that are relevant for you, replace any dot notation by blanks. For the Markdown Preview package, the Grammar string in settings looks like this. It contains the two relevant parts that I used above, but in dot notation:
source.gfm, source.litcoffee, text.html.basic, text.md, text.plain, text.plain.null-grammar
To identify the correct keyboard value, activate the Key Binding Resolver with the shortcut Ctrl+. (Windows) or Cmd+. (Mac) and hit the key combination that you want to catch.
Since you haven't provided a specific example, here's a made up one:
'atom-text-editor[data-grammar="text md"]':
'ctrl+shift+x': 'your-package:command'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With