0

When I close the last sub-window for a file, assuming that it has no sub-windows opened in any other tab, is there a way to have Vim remember all options (comments, textwidth, tabstop, shiftwidth, formatoptions, colorcolumn, wrap, highlight, spell, foldenable, foldmethod, foldcolumn, etc.), as if hidden was set?

I want Vim to use those options if I re-open the file, and I want this to happen for all files. I don't want to set hidden or bufhidden because I want to be forced to write the file if I'm closing the last sub-window.

Afternote: After much perusing of Vim Q&A's and documentation, online and local, I suspect that the solution may lie in using hidden, but also using autocommand with the WinClosed event and some VimScript online to check whether a buffer is opened elsewhere. If not, then find a way to prompt the user to write the file. This is a vague sketch of a possibility. I will post back if I progress the idea.

2
  • If you are setting these options manually, consider making them automatic via ~/.vim/after/ftplugin/<filetype>.vim or with autocommands for specific files?
    – D. Ben Knoble
    Commented Sep 25, 2023 at 19:05
  • The thing is that I seeking this behaviour for all files, regardless of the extension. Sometimes, (say) a *.txt buffer may have shiftwidth=3 and tabstop=3, other times they may be 4. Same with (say) *.py. Whether folders are enabled, the fold method, etc. is dynamic. It may be one thing this hour, something else the next hour, depending on the file. They're not necessarily common across files with the same extension. Does your suggestion still apply in this case? Commented Sep 25, 2023 at 19:10

1 Answer 1

1

That's "Window", not "sub-window".

Also, as long as the buffer is in the buffer list it will keep all its buffer-local options, including comments, textwidth, tabstop, shiftwidth, and formatoptions. This means that you can :w buffer a, switch to buffer b, then come back to buffer a, which will be just as you left it. Those options are gone, though, if you quit Vim.

Other options, like colorcolumn, wrap, spell, foldenable, foldmethod, or foldcolumn are window-local, which means that they are gone when you close the window. They have nothing to do with buffers so it doesn't make sense to tie them to a buffer.

And highlight is global anyway, so it is out of scope.

To me, it looks like you want something like :help modeline, but :help :mkview might give you more flexibility.

Your Answer

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

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