# Shebang filetype detection?

**URL:** <https://discuss.kakoune.com/t/shebang-filetype-detection/1334>\
**Category:** Help\
**Created:** [October 27, 2020, 8:29pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334 "2020-10-27T20:29:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 8:29pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/1 "2020-10-27T20:29:29Z")

</div>

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 :’/

---

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 10:05pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/2 "2020-10-27T22:05:39Z")

</div>

I made my way with

```auto
hook global BufOpenFile .* %{ evaluate-commands %sh{
    if [-z "${kak_opt_filetype}"]; then
        first_line="$(head -n+1 "${kak_buffile}")"

        if [0 -eq "$(expr "$first_line" : "#!")"]; then
            return
        fi

        exec="$(basename "$(echo "$first_line" | awk '{print $1}')")"
        if ["env" != "$exec"]; then
            filetype="$(echo "$exec" | sed "s/[0-9]\+$//g")"
        else
            filetype="$(echo "$first_line" | awk '{print $2}')"
        fi

        printf "set-option buffer filetype '%s'\n" "${filetype}"
    fi
} }

```

---

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 10:23pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/3 "2020-10-27T22:23:53Z")

</div>

Some shebang as /bin/bash give this filetype “bash”. Is just added this to fallback correctly.

```auto
hook global WinSetOption filetype=bash %{
	set buffer filetype sh
}

```

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [October 27, 2020, 10:42pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/4 "2020-10-27T22:42:35Z")

</div>

Awesome ! I though about the same, but never to the time to write anything, so I was very happy to see this post and copy pasted right away in my kakrc.

Any reason you used another hook instead of adding something like:

```bash
        if ["$filetype"!="bash"]; then
            filetype="sh"
        fi

```

?

Good job anyway ! I think you should try to PR it to master

---

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 10:44pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/5 "2020-10-27T22:44:01Z")

</div>

I’d like not to write any dedicated code here. This should just stay stupid

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [October 27, 2020, 10:48pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/6 "2020-10-27T22:48:13Z")

</div>

Good call

It’s really a life saver for all my executable script that don’t have filetype is their name, like all the one in `~/bin` 🙂

---

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 10:52pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/7 "2020-10-27T22:52:51Z")

</div>

I maybe raged 10x per day and I only solved this problem today. Laziness will kill me 🙂

---

<div class="post-metadata">

**Author:** ![ReedWade](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/reedwade/32/633_2.png) [@ReedWade](https://discuss.kakoune.com/u/ReedWade)\
**Post date:** [October 27, 2020, 10:57pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/8 "2020-10-27T22:57:08Z")

</div>

I submited this to the main repo : [https://github.com/mawww/kakoune/pull/3846](https://github.com/mawww/kakoune/pull/3846)

---

<div class="post-metadata">

**Author:** ![scr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/scr/32/486_2.png) [@scr](https://discuss.kakoune.com/u/scr)\
**Post date:** [October 27, 2020, 11:00pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/9 "2020-10-27T23:00:56Z")

</div>

haha same for me 😆

Story of my unix life… I guess I just like raging

---

<div class="post-metadata">

**Author:** ![pepe](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/pepe/32/382_2.png) [@pepe](https://discuss.kakoune.com/u/pepe)\
**Post date:** [November 1, 2020, 11:43am UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/10 "2020-11-01T11:43:32Z")

</div>

Nice! Thanks

---

<div class="post-metadata">

**Author:** ![pepe](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/pepe/32/382_2.png) [@pepe](https://discuss.kakoune.com/u/pepe)\
**Post date:** [November 6, 2020, 1:26pm UTC](https://discuss.kakoune.com/t/shebang-filetype-detection/1334/11 "2020-11-06T13:26:15Z")

</div>

Well, I had the problem, cause my Janet files were recognized as Clojure even with your script. When I looked in Kakoune code I have found, that it uses `file` command, which indeed marked the Janet file as Clojure. So I searched the Clojure magic file and found it can match against the file content.

So creating my own .magic file with the right Janet recognition was actually the easiest way.

Not that anything is wrong with your script, I still have it in my kakrc, but that sometimes, not everything should be solved by the Kakoune :-).
