junegunn / goyo.vim

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

Buffer return to first line after close Goyo using :q #185

Open alihardan opened 5 years ago

alihardan commented 5 years ago

When I close Goyo using :q, buffer cursor jumps to first line of document. But when I use :Goyo, It doesn't jump.

My .vimrc:

call plug#begin('~/.vim/plugged')
Plug 'junegunn/goyo.vim' " Distraction-free writing in Vim. 
call plug#end()
mihalyr commented 4 years ago

I just ran into the same thing and there is a comment in the script saying that switching the line number in the original file does not work when using :q

https://github.com/junegunn/goyo.vim/blob/6b6ed2734084fdbb6315357ddcaecf9c8e6f143d/autoload/goyo.vim#L323-L324

I fixed this with saving line/col on QuitPre and restoring in my goyo_leave (inspired by this example). One thing I don't know yet is why only the line number is restored, but not the column, but that isn't really a problem for me so didn't investigate.

function s:goyo_enter()
    let b:quitting = 0
    autocmd QuitPre <buffer> let b:quitting = 1 | let b:line_pos = line('.') | let b:col_pos = col('.')
endfunction

function s:goyo_leave()
    if b:quitting
        call cursor(b:line_pos, b:col_pos)
    endif
endfunction

autocmd! User GoyoEnter call <SID>goyo_enter()
autocmd! User GoyoLeave call <SID>goyo_leave()
lirorc commented 1 year ago

Thanks mihalyr, solved the problem (for me) :+1: