Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In vim you can open a file under the cursor by using the gf command.

One can also easily open that file in a new split window by hitting <c-w> f. This is a really nice and time saving feature.

However, I can't figure out, how to open the file in an already opened split window (without creating a new one).

share|improve this question
    
You may want to have a look at the preview feature (see :h preview). –  Stephane Chazelas May 3 '13 at 15:01
    
Right, I use preview as well. However I didnt want to open the file in the preview window but explicitely in an other one. –  psibar May 3 '13 at 15:04
add comment

2 Answers

up vote 3 down vote accepted

I got all the pieces together to do the trick. The best way is to create a custom mapping for all the commands:

 map <F8> :let mycurf=expand("<cfile>")<cr><c-w> w :execute("e ".mycurf)<cr><c-w>p

Explanation:

  • map <F8> maps on "F8" the commands that follow
  • let mycurf=expand("<cfile>") gets the filename under the cursor and saves it in mycurf
  • <c-w>w changes the focus to the next open split window
  • execute("e ".mycurf) opens the file saved in mycurf
  • finally <c-w>p changes the focus to the previous window (where we actually came from)
share|improve this answer
add comment

That can't be done easily. A [count] before <C-w>f specifies which file match on 'path' is opened, it does not select an existing window. Only for the <C-w>w command, [count] means "go to existing window number".

To get that functionality, you need to write a custom mapping which either

  • grabs the file, goes to the [count] window and emulates the gf command, or
  • clones the current buffer to the [count] window, and executes gf there
share|improve this answer
    
Yes the only way to do it is to write a custom mapping. I was just having some trouble with grabbing the file under the cursor. But I think I have figured it out now –  psibar May 3 '13 at 14:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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