2

I have been using PyCharm for working on my Django project. Project includes some python scripts.

Those python scripts do not have .py extensions. Just #!/usr/bin/env python at the start of file.

PyCharm doesn't show python syntax highlighting for those. Sublime text 3, detects file those file types and show syntax highlighting perfectly.

Is there any way to configure PyCharm to do that?

I am using PyCharm 4.5.3, professional edition.

Bidhan Roy
  • 441
  • 4
  • 14
  • Why are you not saving them as Python files? – kylieCatt Jul 18 '15 at 15:26
  • If you mean why not with `.py` extension, scripts are moved during project running to somewhere else in system where scripts of definite names are needed. Of course I can rename them while doing so but that's not necessary on sublime. I want it to be same in PyCharm. – Bidhan Roy Jul 18 '15 at 15:30
  • There should be a setting somewhere that associates files of certain extensions (or no extension) with specific syntax highlighters. It usually asks you the first time you open a file it doesn't have a lexer for asks you which one it should use. I'm not a t my home machine and don't have Pycharm installed here so I can't look. – kylieCatt Jul 18 '15 at 15:37
  • What is the current extension for those files? – Leb Jul 18 '15 at 15:42
  • off-topic? Maybe you want to re-read the reason you invoked: `unless they directly involve tools used primarily for programming` then check the `pycharm` tag... – Dan Cornilescu Jul 19 '15 at 13:59

1 Answers1

3

It seems Pycharm (at least the Linux 4.5.3 Pro version) is capable of remembering the "type" of a file without extension if it creates it itself using this flow:

  • File -> New -> File (the plain one, not one from the known formats for which it'll automatically fill in the extension):

enter image description here

  • enter the file name (without extension):

enter image description here

  • in the next window you can associate it with a type:

enter image description here

  • ta-da! - syntax highlighting (and all other python-related features) work:

enter image description here

I couldn't figure out where does pycharm stores this mapping info, so I can suggest a direct method of changing it.

But I think the following workaround method should work (might need some extra work specific to your version control system, if applicable, so that you don't lose the file's change history):

  • rename the original file to a temporary name
  • use the above recipe to create a new file with the original name with the appropriate mapping
  • copy over the file content from the temporary file

Note: make sure you don't accidentally rename the file to a name with a known extension as the mapping will be automatically changed and reverting the name change won't recover the custom mapping.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97