'Vim: How to open new vertical split for file under cursor

I know gf opens the file under cursor, and CTRL-w f opens the file under cursor in a new split window.

I'm probably being greedy but, how do you open it in a new vertical window?



Solution 1:[1]

From normal mode, you could type C-w f C-w L or C-w v gf, and from the command-line you could execute :wincmd f | wincmd L or :vert wincmd f.

They are not strictly equivalent, though.

With C-w f C-w L and :wincmd f | wincmd L, the height of your vertical viewport will be maximized no matter the current layout. So, for example, if you have a horizontal viewport below, its width should be decreased:

        +-----------------+     +--------+-------+
        |  path/to/fileC  |     |        |       |
        |                 |  ?  |        |       |
        |      fileA      |     |  fileA | fileC |
        +-----------------+     +--------+       |
        |                 |     |        |       |
        |      fileB      |     |  fileB |       |
        +-----------------+     +--------+-------+

With C-w v gf and :vert wincmd f, the height of your vertical viewport should be the same as the current one, and thus it shouldn't affect the horizontal viewport below:

        +-----------------+     +--------+-------+
        |  path/to/fileC  |     |        |       |
        |                 |  ?  |        |       |
        |      fileA      |     |  fileA | fileC |
        +-----------------+     +--------+-------+
        |                 |     |                |
        |      fileB      |     |      fileB     |
        +-----------------+     +----------------+

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1