Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 775
Saucier Avatar asked Jan 18 '26 03:01

Saucier


1 Answers

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
like image 138
Ingo Karkat Avatar answered Jan 19 '26 19:01

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!