Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atom editor: How to define key binding only for a certain file type?

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.

like image 665
user3664254 Avatar asked Mar 11 '23 00:03

user3664254


2 Answers

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.

like image 145
Jpsy Avatar answered Apr 06 '23 02:04

Jpsy


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'
like image 36
idleberg Avatar answered Apr 06 '23 02:04

idleberg