# Filetype Detection

**URL:** <https://discuss.kakoune.com/t/filetype-detection/2064>\
**Category:** Recipes & Guides\
**Created:** [May 20, 2022, 9:17pm UTC](https://discuss.kakoune.com/t/filetype-detection/2064 "2022-05-20T21:17:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordan-yee](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/jordan-yee/32/592_2.png) [@jordan-yee](https://discuss.kakoune.com/u/jordan-yee)\
**Post date:** [May 20, 2022, 9:17pm UTC](https://discuss.kakoune.com/t/filetype-detection/2064/1 "2022-05-20T21:17:19Z")

</div>

## Background

I recently ran into an issue where bash (shell) scripts included in a project were not being detected as the sh filetype in Kakoune, and therefore were rendered in plain, white text.

After digging around on this forum and through some issues on GitHub, I was able to figure out the nice, simple (and I assume correct) solution I was looking for. As far as I can tell, how Kakoune detects file types–and what to do if it doesn’t detect a filetype correctly (or at all)–isn’t plainly documented anywhere, so I thought I’d jot down the gist of my findings here…

- Kakoune detects filetypes using the `file.kak` script bundled with the standard rc scripts that ship with Kakoune.
- This uses the assigned mime-type of a given file, using the unix `file` command. See `man file`.
- If the mime-type obtained via the `file` command isn’t giving you the right file type, you can add custom rules for it to use to a `magic` file. See `man magic` **(this will provide the info you will probably need to define a custom file detection rule)**.

## Example

Now here’s a mini-guide/example based on what I needed to do…

My problem was that the bash scripts I was looking at had the following shebang, with no file extension:

```auto
#!/usr/bin/env -S bash

```

To add a custom rule for this, I created a `.magic` file in my home directory:

```auto
0	string	#!/usr/bin/env\ -S\ bash	Alternative bash shebang
!:mime text/x-shellscript

```

The template for this rule is:

```auto
<offset> <type> <test> <message>
!:mime <mime-type>

```

Some additional notes on this example:

- I simply copied the shebang rule and escaped the spaces for the test.
- See the `file.kak` rc script for a list of mime-types that Kakoune recognizes.
  - For a default installation use: `kak /usr/local/share/kak/rc/detection/file.kak`

- `man magic` provides the in-depth details on how to define “magic files”

## Related links

> [@Shebang filetype detection?](https://discuss.kakoune.com/t/shebang-filetype-detection/1334):
>
> Does anyone got a BufOpenFile hook to detect filetype with shebang ? Something that could match “#!/usr/bin/sh” and “#!/usr/bin/env sh” I’m pretty sure this could be done with two or three line but I sucks so much :’/

> <https://github.com/mawww/kakoune/pull/3846>
>
> This work with filetypes as
> 
> \* #!/bin/sh
> \* #!/bin/sh -x
> \* #!/bin/env sh
> \* #…!/bin/env python
> \* #!/bin/env python3
> 
> This will strip last digit to transfrom python3 to python
> 
> This will cause trouble with shebang as /bin/bash as bash is not
> the correct filetype which is sh.
> 
> You still can use this to map this kind of filetype
> 
> hook global WinSetOption filetype=bash %{
> set buffer filetype sh
> }
> 
> edit: a mapping has been added to handle python3 and bash execs.

> <https://github.com/mawww/kakoune/pull/711>
>
> This script reads the first line of the buffer and does a stupid&simple
> associat…ion between the last word (excluding interpreter flags) and the
> filetype to assign to the buffer.

> <https://github.com/mawww/kakoune/issues/3982>
>
> \### Steps 
> 
> Open file with a shebang of 
> 
> \`\`\`
> \#!/usr/bin/env zsh
> \`\`\`
> 
> \##…# Outcome 
> 
> Doesn't syntax highlight
> 
> \### Expected 
> 
> Syntax highlighting
> 
> \### Additional Context
> 
> I tried the solution referenced in \[this\](https://github.com/mawww/kakoune/issues/2858) issue, but it didn't work. I am also on OSX which I think makes things a little different. Thanks!

---

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [May 21, 2022, 8:07am UTC](https://discuss.kakoune.com/t/filetype-detection/2064/2 "2022-05-21T08:07:29Z")

</div>

yeah by default `file` recognizes shebangs like `#!/usr/bin/env bash` but not `#!/usr/bin/env -S bash`.  
We should fix this [upstream](https://www.darwinsys.com/file/) by including the relevant variations.
