vimfile-typeautocmd

Run/Toggle command for certain filetypes only


I found this nice plugin for distraction free writing named Goyo, which is really well done. I setup autocmds to enable Goyo based on the filetype, so if I work on a markdown or textfile Goyo gets initialized automatically. If I leave the buffer or change the filetype then Goyo gets closed. Below is how I implemented the behaviour:

autocmd FileType * :Goyo!
autocmd FileType markdown :Goyo
autocmd FileType text :Goyo

That seems to work fine. The question is, whether or not this is the way to go or if there is a better approach to solve the problem?


Solution

  • That's just fine and how I would implemented it, too. As you only hook into the FileType event, the toggling is only triggered when you :edit a new file, not when you recall an existing buffer with another filetype. You could do that with BufWinEnter, but it may cause too many inadvertent togglings. I guess the plugin comes with a quick toggle mapping to manually do this, anyway.

    Alternative

    An alternative to the autocmd FileType commands is filetype plugins (i.e. ~/.vim/ftplugin/markdown.vim etc.), which have the benefit of separating things neatly. But as you need a catch-all autocmd to turn off Goyo, and the list of filetypes is small, I would also prefer keeping things together, just like you did.

    Improvements

    Note that your set of commands would add a duplicate set of autocmds if you re-:source your ~/.vimrc (or whichever script you've put them in). To avoid that, you could wrap them in

    augroup AutomaticGoyo
        autocmd!
        ...
    augroup END