1

I was trying to setup NeoVim to autosave each time a change is made. I used this command, which I wrote in ~/.config/nvim/init.vim:

autocmd TextChanged, TextChangedI * silent write

It works perfectly for what I needed, but I found it to get in conflict with nerdtree, which I set-up with the following:

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

Basically, it starts nerdtree at Vim start-up whenever I am not editing a specific file.

The error I am encountering is that the autosave command tries to save when I am in Nerdtree as well; for this reason, Vim gives the following error:

Error detected while processing TextChanged Autocommands for "*":

E382: Cannot write, 'buftype' option is set.

Press ENTER or type command to continue

Of course, if I press enter, it disappears, but whenever I press something to navigate in Nerdtree, it shows up again and it is very annoying.

Is there a way to fix this problem, maintaining the autosave?

1

1 Answer 1

2

You can update your autocmd to check whether the buffer is of a type that can accept the :write command.

For example:

autocmd TextChanged,TextChangedI *
    \ if &buftype ==# '' || &buftype == 'acwrite' |
    \     silent write |
    \ endif

See :help 'buftype' to see the types of buffer and particularly the ones you might want to :write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.