9

Is there a way to make vim (or nvim) open all new splits vertically by default? There's splitright to control which side vertical splits open on, but as far as I can tell there's no built in option to make splits vertical by default. Does anyone know of a workaround? I found one for the help command specifically, but I'm looking for something that will make all splits vertical.

6 Answers 6

5

There's not really an option that does that.

You can accomplish that by using an autocmd, more specifically catching the WinNew event and moving the newly created window so that it's a vertical one rather than horizontal.

For example:

autocmd WinNew * wincmd L

(Don't forget to add this to an appropriate augroup and clear the augroup first, so if you reload your vimrc it won't duplicate the rules.)

Note also that this isn't always the same as using vertical, since wincmd L will always make the new window the rightmost vertical top-level window. It's also somewhat hard to override it (you can use the :noautocmd modifier, but that will disable all autocmds.)

But perhaps this is close enough to what you're looking for.

2
  • 1
    This has gotten me closer to the behavior that I want than any other suggestion I've seen; thanks.
    – Morgan May
    Feb 8, 2020 at 3:16
  • You're very welcome @MorganMay! Glad I could help. By the phrasing of your question, I felt this might have been what you were after...
    – filbranden
    Feb 8, 2020 at 4:22
8

:split, :new and such are "horizontal" commands. But they all have "vertical" counterparts: :vsplit, :vnew etc. Take a habit to choose the right ones.

Also, you're not allowed to redefine Vim builtin commands (except a few starting with a capital letter), but, if you wish, you can redefine key mappings, such as nnoremap <c-w>n <c-w>v and so on.

5

Unfortunately, this is not possible and not all commands that open windows (e.g. :sp), have vertical counterparts (e.g. :vsp).

So a couple of years ago, I made the splitvertical patch, that introduces the 'splitvertical' option.

Unfortunately, it has never been included.

1

There’s no option because there’s an specific command: :vsplit (:vs). It takes a filename as an argument, just as :sp does. In fact the option you mentioned, splitright, affects the behaviour of :vs.

1

You can use <C-W> f to open the filename under the cursor into a new split. Its behavior is similar to :split followed by gf. You can also use <C-W> <C-F> for the exact same behavior.

Thus, I propose remapping the second one to a vertical split:

nnoremap <C-W><C-F> :vsplit<CR>gf

Its behavior will be different than <C-W> f if the filename can't be found, as this remapping will still open a new vertical split, while <C-W> f won't.

1

Since using :vsplit is easy enough, I don't see the point to interfere within vim configurations. Other suggestions in the answers in this page are just as easy. So, for the main point, I'd suggest to learn the vertical flavor of the commands.

But here an alias you might want. I always use vim -O file1 file2 ... to open files vertically. This could be shortened by editing your ~/.bash_aliases and adding:

alias vvi="vim -O"

This will allow you to simple type, for example to open some .c files in a directory:

$ vvi *.c

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.