1

Background

  • Vim 7.3 all platforms.
  • Vim allows the user to open a file "hyperlink" by pressing gf.

Problem

Goal

Find a solution that makes gf work with tab-page as the default behavior.

Details

The current default behavior opens the new file in the same viewport, but sometimes a user might want to open the new file in a different tab using the Vim tab-page feature.

Alternatively, a user may wish to always have gf use tab-page and never open the linked file in the same viewport.

Vim allows the gf command to open in a new tab, but it requires the user to type control-W <C-W> before invoking gf.

The existing approach is not optimal, because the keystrokes are cumbersome and introduce the risk of repetitive strain injury for users who make heavy use of the gf command.

See also

Vim help:

  • :help gf
  • :help tab-page-intro
  • :help tab-page
  • :help tab-page-commands | /_gf
  • :help CTRL-W_gf

Desired solution

It would be desirable to do away with the need to type <C-W>gf all the time, in order to save keystrokes and reduce the risk of repetitive strain injury.

Failed attempt

The following attempt did not work (use a normal command):

:normal <C-W>gf

Questions

  • why does not :normal <C-W>gf work as expected?
  • what alternative approaches will work to make Vim exhibit the desired behavior and reach the goal?
dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • 2
    Also read the last couple of paragraphs of `:h normal` – FDinoff Jun 15 '14 at 21:20
  • `:help normal | /Example:` (another way to find it, thanks to @FDinoff for the additional tip). – dreftymac Jun 15 '14 at 21:25
  • Obligatory [learn to love buffers](http://stackoverflow.com/questions/102384/using-vims-tabs-like-buffers) and [use them effectively](http://stackoverflow.com/questions/21331664/how-to-show-tab-close-button-in-gvim/21338192#21338192) comment. – Peter Rincker Jun 16 '14 at 14:04
  • @PeterRincker: Great links, but the statement *Tab pages are great. However it all comes down to vim treating tab pages as more of view-ports into a group of windows or workspaces* does not take into account people who already recognize this fact about Vim and use tabs anyway to augment the power of buffers. – dreftymac Jun 28 '14 at 01:56
  • @dreftymac Vim is buffer-centric. It prefers to use buffers over tabs. In my opinion tabs are great for setting up a highly specialized separate layout, for completely different projects, or holding some scratch buffers. Outside of that I have a hard time justifying the usage of tabs as they just do not scale well for me or my workflow. Tabs may work great for you. However I doubt a commonly used command like `gf` would work better for me if it used tabs. I imagine it would lead to too many open tabs and be a small barrier to a good split window workflow – Peter Rincker Jun 30 '14 at 14:46
  • @PeterRincker **//Vim is buffer-centric.//** Agreed. **//It prefers to use buffers over tabs.//** That's definitely one way of seeing it, however any user who has enough experience with Vim can attest that the editor is powerful enough to adjust to a wide and varying range of preferences. Another way of looking at it: tabs in Vim are much more powerful than tabs in most other contexts, precisely because they augment the extremely flexible buffer system. Considering this, the only challenge is to get users to realize that Vim tabs are not a substitute for buffers, but rather an augmentation. – dreftymac Jul 02 '14 at 09:02

1 Answers1

4
  • The answer to your first question:

    What comes after :normal is supposed to be raw key presses, as you'd type them in normal mode. What you used means nothing so it does nothing (you are lucky, it could mean something and fuck up your entire buffer!).

    It would work if you inserted the raw representation of <C-w>:

    :norm <C-v><C-w>gf
    

    which should look like:

    :norm ^wgf
    
  • The answer to your second question:

    nnoremap gf <C-w>gf
    
romainl
  • 186,200
  • 21
  • 280
  • 313