Search code examples
pycharmjetbrains-idelinterblack-code-formatter

Is there a way to define keymap shortcuts for external tools depending on the file type, in PyCharm?


One use case would be to have the black code formatter for python files, and the sqlfluff code formatter for SQL files, and to use the same keyboard shortcut for both tools.

Currently, both are set up in Preferences -> Tools -> External Tools, following black documentation.


Solution

  • Sadly no. You cannot assign one shortcut and invoke a different External Tool based on the file extension.

    But you can workaround it pretty easily:

    • You will need to have a single External Tools entry instead of the current two.
    • Make a shell/batch script that will do the actual formatting (will call the actual formatter tool).
    • Use that script as an External Tools command.
    • In that custom script detect what extension the file has and execute the most appropriate formatter there (black for Python files and sqlfluff for SQL files).