Search code examples
vimhyperlink

vim goto file in new tab-page as the default behavior


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?

Solution

    • 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 ruin 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