3

TL;DR:

I want to avoid split windows and tabs. Is there a way to intercept a split command with an autocommand that translates every window split, including those of plugins, into a simple buffer switch? Instead of splitting the window, I want to open the buffer that would have appeared in the split in the main window, i.e. full-screen.

Longer version:

Because I have low vision and therefore require text to be quite large, split windows don't make sense for my workflow. The split windows themselves become way too small to be usable.

That put me on the quest to set up my (Neo) Vim config such that anything I do could be done from within the main window without any splits.

What has been working well for me is the following autocommand (I'd like to credit the source here, but I sadly don't remember where I got the idea from) which will open every new window - that includes splits - in a new tab.

vim.api.nvim_create_autocmd("WinNew", {
    group = "bufcheck",
    pattern = "*",
    command = "if winnr() != winnr('$') |wincmd T"
})

But what if I want to avoid tabs and instead just switch buffers?

Is there an autocommand that can be triggered before a window is created?

Is there a smarter way to avoid window creation altogether?

Many thanks!

4
  • 3
    Hint: the autocmd event is still the correct one. Just needs to edit the action to be done on the event. Instead of wincmd T, which opens the current window in a new tab, you have to execute a command that makes the current split/window the only window.
    – 3N4N
    Commented Aug 17, 2023 at 5:02
  • @romainl I removed the [neovim] tag because the problem isn't specific to neovim and neither is my answer. What's your reasoning for adding it back in?
    – Rich
    Commented Aug 17, 2023 at 10:59
  • Reducing the background noise.
    – romainl
    Commented Aug 17, 2023 at 13:15
  • @romainl What background noise?
    – Rich
    Commented Aug 18, 2023 at 9:07

1 Answer 1

2

As 3n4N hints in the comments, your code is 90% of the way there; you just need to replace the command that opens the window in a new tab with one that makes the current window the only window.

vim.api.nvim_create_autocmd("WinNew", {
    group = "bufcheck",
    pattern = "*",
    command = "only"
})

The above is untested because I don't have Neovim, but the Vimscript version works fine:

augroup bufcheck
  autocmd!
  autocmd WinNew * only
augroup END
1
  • Just adding to this myself. Using the only command, as @Rich points out, appears to be the way to go! There is an issue with plugins where closing a temporary window, e.g. the sync status of packer by hitting q as packer suggests, would close the whole editor. I'm currently solving this with a new mapping - <leader>q - which will close the current buffer instead of the window. If the buffer is read-only, it'll be a bd; otherwise w|bd. I hope this helps, and let me know if there is a better way to address this!
    – TheGiwi
    Commented Aug 19, 2023 at 12:19

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.