# How can I set the indentwidth option for certain file types?

**URL:** <https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241>\
**Category:** Help\
**Tags:** format\
**Created:** [December 19, 2018, 2:03pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241 "2018-12-19T14:03:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbrains](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/jbrains/32/61_2.png) [@jbrains](https://discuss.kakoune.com/u/jbrains)\
**Post date:** [December 19, 2018, 2:03pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/1 "2018-12-19T14:03:25Z")

</div>

I like my default indentation width at 2, but sometimes I want 4, depending on the file type. Can I set the indentwidth option for all files of a given type?

---

<div class="post-metadata">

**Author:** ![eraserhd](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/eraserhd/32/15_2.png) [@eraserhd](https://discuss.kakoune.com/u/eraserhd)\
**Post date:** [December 19, 2018, 3:16pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/2 "2018-12-19T15:16:34Z")

</div>

It’s something like:

```auto
set-option global indentwidth 2
hook global WinSetOption filetype=java {
  set-option window indentwidth 4
}

```

---

<div class="post-metadata">

**Author:** ![Delapouite](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/delapouite/32/10_2.png) [@Delapouite](https://discuss.kakoune.com/u/Delapouite)\
**Post date:** [December 19, 2018, 5:16pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/3 "2018-12-19T17:16:51Z")

</div>

Kakoune also offers basic support for [https://editorconfig.org/](https://editorconfig.org/)

> <https://github.com/mawww/kakoune/blob/master/rc/extra/editorconfig.kak>

---

<div class="post-metadata">

**Author:** ![TeddyDD](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/teddydd/32/21_2.png) [@TeddyDD](https://discuss.kakoune.com/u/TeddyDD)\
**Post date:** [December 19, 2018, 8:56pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/4 "2018-12-19T20:56:47Z")

</div>

> [@Delapouite](#):
>
> basic support for [https://editorconfig.org/](https://editorconfig.org/)

If I recall correctly It’s complete support excluding `insert_final_newline` since Kakoune always inserts newline. Is anything missing?

---

<div class="post-metadata">

**Author:** ![jbrains](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/jbrains/32/61_2.png) [@jbrains](https://discuss.kakoune.com/u/jbrains)\
**Post date:** [December 20, 2018, 2:10pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/5 "2018-12-20T14:10:34Z")

</div>

> [@eraserhd](#):
>
> hook global WinSetOption filetype=java { set-option window indentwidth 4 }

“no window in context”, so I guess I need to read about hooks now. Thanks for pointing me in a promising direction.

---

<div class="post-metadata">

**Author:** ![jbrains](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/jbrains/32/61_2.png) [@jbrains](https://discuss.kakoune.com/u/jbrains)\
**Post date:** [December 20, 2018, 2:23pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/6 "2018-12-20T14:23:53Z")

</div>

I did this exactly:

```auto
hook global WinSetOption filetype=java %{ set-option buffer indentwidth 4 }
```

Question: do I need to know anything about the difference between calling set-option on the buffer compared to on the window?

---

<div class="post-metadata">

**Author:** ![eraserhd](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/eraserhd/32/15_2.png) [@eraserhd](https://discuss.kakoune.com/u/eraserhd)\
**Post date:** [December 20, 2018, 2:55pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/7 "2018-12-20T14:55:59Z")

</div>

I’ve thought it weird to set these things on windows instead of buffers, but I think this is how Maxime is intending it to be used. Right now, some options _can_ stick around if the window switches to another buffer. Maxime is planning a mechanism to make that not happen. Some kind of setting a shared group of options and then selecting which set to use for a window, then unlinking that set.

---

<div class="post-metadata">

**Author:** ![occivink](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/occivink/32/27_2.png) [@occivink](https://discuss.kakoune.com/u/occivink)\
**Post date:** [December 20, 2018, 9:07pm UTC](https://discuss.kakoune.com/t/how-can-i-set-the-indentwidth-option-for-certain-file-types/241/8 "2018-12-20T21:07:10Z")

</div>

> [@jbrains](#):
>
> `hook global WinSetOption filetype=java %{ set-option window indentwidth 4 }`

This hook should work (note the % that was missing), but it will only take effect the next times `filetype` is set to java. So if you do run it interactively it won’t take effect on the existing “windows” (see :doc scopes for an explanation). If you add it to your kakrc it should affect all files detected as java.
