0

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?

ReneFroger
  • 484
  • 8
  • 20
  • I suggest using less tabs and learning to use [buffers effectively](http://stackoverflow.com/a/21338192/438329) – Peter Rincker Apr 02 '14 at 21:51
  • Thanks Peter for your reply. I will read it. But I don't get it why I'm not having this issue, when I do use ctrl+PageDown manually. It seems my guess of this behaviour is not right. When you open more tabs, you see that Vim still jump back to the third tab. – ReneFroger Apr 02 '14 at 21:57
  • Peter, I read your link about buffers. To me, this is a bug, not "user error". Searching around the web for 'vim tabs' indicates that that just about everyone else disagrees with you, or is unaware of the "real" way to use Vim tabs. Also, if tabs are really "layout" views, then why are default tab titles, the current file name? If I wanted multiple views of the same file, then all my tab titles would be same name (not very helpful?). So either way you look at it, the default tab implementation in Vim is flawed, if I am right? – ReneFroger Apr 02 '14 at 22:07
  • IMHO, it is a right of passage for every new vimmer to misuse tabs. Therefore there are many articles on the web on how to use vim's tabs in certain ways. In fact vim only quite recently received tabs and has gotten along swimmingly beforehand with buffers. I find it hard to believe "everyone" disagrees with me considering the links I provided in that post as I link to the popular Vimcasts website and another is a talk by Bram Moolenaar (Vim's creator). In the end you can tailor Vim to your needs. However Vim is opinionated software with a "Vim Way". – Peter Rincker Apr 02 '14 at 22:20
  • I agree with you. I found the following blog useful, http://blog.sanctum.geek.nz/buffers-windows-tabs/ Especially when you come from another editors with tabs that stands for opened files. Much to learn about Vim. I'm wondering now if there is anyone who knows everything in Vim.... – ReneFroger Apr 02 '14 at 22:46

2 Answers2

2

<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.

Community
  • 1
  • 1
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • Peter, thanks for your insightful reply. Now I get more insight why this problem is performing. But the test0.html after opening first file, is still the last tab. Because you're inside, right? And unfortunately, your solution is not working. The execute command is not recognizing the gF, and treats it like a variable. But nevertheless, I would like to say my appreciation for your efforts here. :) – ReneFroger Apr 02 '14 at 22:38
  • Sorry was in a hurry. Forgot the `:normal` and to escape the keycodes. – Peter Rincker Apr 02 '14 at 22:50
  • I found already another solution. Just use noremap gf gF and it will have the effect. My fault. But I will use your solution, because you seems the expert here. Many thanks for your effort! – ReneFroger Apr 02 '14 at 22:57
0
noremap gf :tabe<cfile><CR><c-PageUp>

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

ReneFroger
  • 484
  • 8
  • 20