junegunn / goyo.vim

:tulip: Distraction-free writing in Vim
MIT License
4.49k stars 115 forks source link

GoyoEnter not being executed when called with autocmd #226

Open MrVizious opened 4 years ago

MrVizious commented 4 years ago

Hello everyone.

I am trying to execute Goyo automatically with any markdown file (I've seen it asked many times online, so nothing new). However, I also want number and relativenumber to be on when in Goyo mode. I tried this part of the wiki to execute a couple of commands when starting Goyo, but it only works when I manually enter the Goyo command.

This is what my relevant part of goyo.vim looks:

function! s:goyo_enter()
    let b:quitting = 0
    let b:quitting_bang = 0
    set number
    set relativenumber
    set scrolloff=999
    autocmd QuitPre <buffer> let b:quitting = 1
    cabbrev <buffer> q! let b:quitting_bang = 1 <bar> q!
endfunction

function! s:goyo_leave()
    " Quit Vim if this is the only remaining buffer
    if b:quitting && len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
        if b:quitting_bang
            qa!
        else
            qa
        endif
    endif
endfunction

let &cpo = s:cpo_save
unlet s:cpo_save

autocmd! User GoyoEnter call <SID>goyo_enter()
autocmd! User GoyoLeave call <SID>goyo_leave()

And I have tried the autocmd feature with these two options in my init.vim file:

:autocmd BufReadPost *.md silent! Goyo 80
:autocmd Filetype markdown Goyo

I have not used them both at once, of course.

Whenever I open a markdown file, a normal Goyo instance opens, but without the number options set. Also, I have to press :q twice in order to exit.

Any help would be appreciated. Thanks in advance!