Vim Tips Wiki
(list related tips; use table)
Line 12: Line 12:
 
}}
 
}}
 
The following commands open the file name under the cursor:
 
The following commands open the file name under the cursor:
  +
{| class="cleartable"
*<tt>gf</tt> &nbsp;&nbsp; open in the same window ("goto file")
 
*<tt><c-w>f</tt> &nbsp;&nbsp; open in a new window (Ctrl-w <tt>f</tt>)
+
|<tt>gf</tt> || open in the same window ("goto file")
  +
|-
*<tt><c-w>gf</tt> &nbsp;&nbsp; open in a new tab (Ctrl-w <tt>gf</tt>)
 
 
|<tt><c-w>f</tt> || open in a new window (Ctrl-w <tt>f</tt>)
  +
|-
 
|<tt><c-w>gf</tt> || open in a new tab (Ctrl-w <tt>gf</tt>)
  +
|}
   
 
When writing a program, it is helpful to set the <tt>'path'</tt> option to list the directories with your include files. Then you can easily open an include file.
 
When writing a program, it is helpful to set the <tt>'path'</tt> option to list the directories with your include files. Then you can easily open an include file.
Line 37: Line 41:
 
</pre>
 
</pre>
 
(or similar) in order to access <tt>C:\Documents and Settings\My User Name\My Documents</tt>. (Note that double backslashes are used here in the <tt>:set</tt> command to get single backslashes in the value. Alternately, ''forward'' slashes could have been used inside Vim.
 
(or similar) in order to access <tt>C:\Documents and Settings\My User Name\My Documents</tt>. (Note that double backslashes are used here in the <tt>:set</tt> command to get single backslashes in the value. Alternately, ''forward'' slashes could have been used inside Vim.
  +
  +
==Related tips==
  +
''Probably need to merge some of following; those that should be separate can be moved to "See also".''
  +
*[[VimTip226|226 Edit file under cursor after a horizontal split]]
  +
*[[VimTip384|384 Easily switch between source and header file]]
  +
*[[VimTip487|487 Jump to a file to a certain line number]]
  +
*[[VimTip691|691 Use gf to open a file via its URL]]
  +
*[[VimTip985|985 Quickly Get Files in your Environment]]
  +
*[[VimTip1546|1546 Automatically add Python paths to Vim path]]
   
 
==References==
 
==References==

Revision as of 07:36, 6 January 2010

Tip 299 Printable Monobook Previous Next

created 2002 · complexity basic · version 7.0


The following commands open the file name under the cursor:

gf open in the same window ("goto file")
<c-w>f open in a new window (Ctrl-w f)
<c-w>gf open in a new tab (Ctrl-w gf)

When writing a program, it is helpful to set the 'path' option to list the directories with your include files. Then you can easily open an include file.

If there are several files in your 'path' that match the name under the cursor, gf opens the first, while 2gf opens the second, and 3gf opens the third, etc.

You can return to the previous buffer using Ctrl-^ or Ctrl-o.

Names containing spaces

You may need to open a file that has a space in its name. One approach is to visually select the name (including its embedded spaces), then type gf. For example, you could position the cursor on the first character of the file name, then press v to start visual selection, then repeatedly press E to move to the end of the next WORD until the whole name is selected, then type gf to go to the file.

Another approach is to adjust the isfname option.

To have a space (ASCII 32) considered as a valid character for a file name, add the following to your vimrc:

:set isfname+=32

On Dos/Windows (only), a further approach is to use the 8.3 form of the path to avoid spaces:

:set path+=C:\\DOCUME~1\\MYUSER~1\\MYDOCU~1

(or similar) in order to access C:\Documents and Settings\My User Name\My Documents. (Note that double backslashes are used here in the :set command to get single backslashes in the value. Alternately, forward slashes could have been used inside Vim.

Related tips

Probably need to merge some of following; those that should be separate can be moved to "See also".

References

Comments

You can list the files in 'path' that match the name under the cursor with:

:echo globpath(&path, expand('<cfile>'))

The plugin searchInRuntime overrides gf and CTRL-W_f to ask which file must be opened if several match. Alternatives for :sp and :vsp are also provided.