Although you probably already know how it works, this little article explains it very well.
For more information :help ftplugin
.
I would love to write this section in Lua, but it is not natively supported by NeoVim yet. Although you can use plugin like astronauta.nvim, I think it is too much to install a plugin for something so simple. I hope that in the future this option will be in the NeoVim core.