Key bind dependent on currently open file


Let’s say I have c/cpp source file and .py file. When I have c/cpp file open I want to be able to compile it using F5 button, and when I have .py file open I want to run Python interpreter using the same key. Is it possible to change key bind depending on file I’m currently in? Currently I have to manually change key binding.


You can bind the key to a specific grammar. As an example to run custom:custom-command when pressing ctrl-. in a file using C++ grammar you can add the following to your keymap:

"atom-text-editor[data-grammar='source cpp']":
  'ctrl-.': 'custom:custom-command'


If you’re using the script or atom-runner packages, you can have multiple binaries associated with the same command.