Question

Vim is so awesome. For example, you have a file, called 'test0.html', stored in a folder.

In the same folder, you store the folder 'test' with the files test1.html, and test2.html.

Then you put in test0.html the following content:

include('test/test1.html');
include('test/test2.html');

In vim, you put the cursor on the filenames. You open the files under the corsor with the keys gf. Thats why Vim is so awesome. I would like to open in a new tab. That's possible with the keys gF. But what if you want to stay in the same file, but open the file in a background tab, like Chrome does? So I'm mapping the key.

noremap gf <c-w>gF<c-PageDown>  

So, when my cursor is on test1.html, it open with the key gf in a background tab. Wonderful, now I'm a satisfied man. Then I want to open test2.html under cursor.

Vim jumps to the tab of test1.html, instead stay on test0.html When I tried to debug this weird behaviour, by only mapping gf to gF, and then do manual CTRL+pagedown, I get the expected behaviour.
My guess is that Vim is much faster with executing the command before he opens the new tab (gF), and so I get to the last tab from the first tab.

Am I correct in my guess explaination?

Was it helpful?

Solution 2

noremap gf :tabe<cfile><CR><c-PageUp>

This is even better. When the file doesn't exist, Vim will create a new one.

OTHER TIPS

<c-PageDown> or more commonly used gT will got to the previous tab. <c-w>gF on the other hand will open the file under the cursor in a new tab. That tab will be last tab. So doing a gT will not always make you go back to the previous tab.

You can change your mapping to go back to the previous tab like so:

nnoremap gf :execute "normal! \<lt>c-w>gF" . tabpagenr() . "gt"<cr>

However I would personally suggest you avoid using tabs in such a manner and use buffers instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top