Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do something like gf, but in a new vertical split?

Tags:

In vim gf opens the file whose name is under the cursor in the current window. <C-W>f does the same but opens it in a new window. However this new window is created with an horizontal split.

How can I do the same and get a vertical split?

I tried various alternatives that did work (like :vsplit +normal\ gf), but have a slight problem: if the file doesn't exist, a new window is created anyways. This does not happen with gf nor <C-W>f. I'd like to have this behaviour as well from the "open file under cursor on a vertical split" command. How can I do that?

like image 293
R. Martinho Fernandes Avatar asked Oct 06 '11 10:10

R. Martinho Fernandes


3 Answers

Here is possible mapping:

:nnoremap <F8> :vertical wincmd f<CR>

With a file name under cursor, hit F8 and voila.

like image 184
mloskot Avatar answered Nov 06 '22 21:11

mloskot


Here is a solution involving 5 key strokes:

  • Ctrl-wv
  • gf

That is Ctrl-wv followed by gf in the normal mode.

Here is an equivalent solution that involves 7 key strokes:

  • :vs
  • gf

That is :vs command in command-line mode followed by gf in normal mode. Assuming we are in the normal mode with the cursor on the filename already, the complete sequence of keystrokes are: Shift:vsEntergf.

When we enter Ctrl-wv in normal mode or :vs in the command-line mode, the current window is split into two with the same file in both and the cursor remains in the same position (i.e., on the filename of the file that we want to go to in a new vertical split). So if we press gf now, the current window is now updated with the file we want to go to.

The end result is two vertical split windows: one with the first file and another with the file we wanted to go to.

like image 27
Susam Pal Avatar answered Nov 06 '22 19:11

Susam Pal


If we're playing Vim Golf, I think the winning solution is to do:

Ctrl-w then f

to open the relevant file in a horizontal split.

Then change it to a vertical split, send then active window to the left or right side of the screen by doing either

Ctrl-w then L (active window to the left)

or

Ctrl-w then R (active window to the right)

Note that the final keystroke for left / right must be uppercase. The others should be lowercase

like image 34
Noel Evans Avatar answered Nov 06 '22 21:11

Noel Evans