Vim Tips Wiki
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Often a file contains the name of a second file, and you would like to open the second file. Do that by putting the cursor on the file name and typing <code>gf</code>. This tip explains the details of <code>gf</code>.
+
Often a file contains the name of a second file, and you would like to open the second file. Do that by putting the cursor on the file name and typing <tt>gf</tt>. This tip explains the details of <tt>gf</tt>.
   
 
==Go to file==
 
==Go to file==
 
The following commands open the file name under the cursor:
 
The following commands open the file name under the cursor:
 
{| class="cleartable"
 
{| class="cleartable"
|<code>gf</code> || open in the same window ("goto file")
+
|<tt>gf</tt> || open in the same window ("goto file")
 
|-
 
|-
|<code><c-w>f</code> || open in a new window (Ctrl-w <code>f</code>)
+
|<tt><c-w>f</tt> || open in a new window (Ctrl-w <tt>f</tt>)
 
|-
 
|-
|<code><c-w>gf</code> || open in a new tab (Ctrl-w <code>gf</code>)
+
|<tt><c-w>gf</tt> || open in a new tab (Ctrl-w <tt>gf</tt>)
 
|}
 
|}
   
We usually type <code>gf</code> in normal mode in which case Vim has to determine where the file name starts and ends. It does that by taking all characters matching the <code>'isfname'</code> option. Alternatively, you can visually select the file name, then type <code>gf</code> in which case Vim will use the selected text.
+
We usually type <tt>gf</tt> in normal mode in which case Vim has to determine where the file name starts and ends. It does that by taking all characters matching the <tt>'isfname'</tt> option. Alternatively, you can visually select the file name, then type <tt>gf</tt> in which case Vim will use the selected text.
   
With the cursor on a file name, pressing Ctrl-w then <code>f</code> (or Ctrl-f) will open the file in a new window (after a horizontal split). If you want a vertical split, you could next type Ctrl-w <code>L</code> to move the window to the right. Or, you could map a key to perform a vertical "go to file" split (<code>:vertical</code> makes what follows split vertically; <code>:wincmd</code> is a command that is equivalent to pressing Ctrl-w):
+
With the cursor on a file name, pressing Ctrl-w then <tt>f</tt> (or Ctrl-f) will open the file in a new window (after a horizontal split). If you want a vertical split, you could next type Ctrl-w <tt>L</tt> to move the window to the right. Or, you could map a key to perform a vertical "go to file" split (<tt>:vertical</tt> makes what follows split vertically; <tt>:wincmd</tt> is a command that is equivalent to pressing Ctrl-w):
 
<pre>
 
<pre>
 
:map <F8> :vertical wincmd f<CR>
 
:map <F8> :vertical wincmd f<CR>
 
</pre>
 
</pre>
   
When writing a program, it is helpful to set the <code>'path'</code> 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.
   
If there are several files in your <code>'path'</code> that match the name under the cursor, <code>gf</code> opens the first, while <code>2gf</code> opens the second, and <code>3gf</code> opens the third, etc.
+
If there are several files in your <tt>'path'</tt> that match the name under the cursor, <tt>gf</tt> opens the first, while <tt>2gf</tt> opens the second, and <tt>3gf</tt> opens the third, etc.
   
 
You can return to the previous buffer using Ctrl-^ or Ctrl-o.
 
You can return to the previous buffer using Ctrl-^ or Ctrl-o.
   
 
===File name and line number===
 
===File name and line number===
Instead of just a file name, you may have a name followed by a line number. For example, <code>global.h:123</code> indicates line 123 in file <code>global.h</code>. Instead of a colon, any whitespace and another delimiter character can separate the name and number; the delimiter must not be a character expected in a file name (the <code>'isfname'</code> option must not include the delimiter).
+
Instead of just a file name, you may have a name followed by a line number. For example, <tt>global.h:123</tt> indicates line 123 in file <tt>global.h</tt>. Instead of a colon, any whitespace and another delimiter character can separate the name and number; the delimiter must not be a character expected in a file name (the <tt>'isfname'</tt> option must not include the delimiter).
   
On Windows, the default <code>'isfname'</code> includes a colon. If you do not use drive letters to identify files, you could remove the colon with the command:
+
On Windows, the default <tt>'isfname'</tt> includes a colon. If you do not use drive letters to identify files, you could remove the colon with the command:
 
<pre>
 
<pre>
 
set isfname-=:
 
set isfname-=:
Line 46: Line 46:
 
The following commands open the file with the cursor on the specified line number:
 
The following commands open the file with the cursor on the specified line number:
 
{| class="cleartable"
 
{| class="cleartable"
|<code>gF</code> || open in the same window
+
|<tt>gF</tt> || open in the same window
 
|-
 
|-
|<code><c-w>F</code> || open in a new window (Ctrl-w <code>F</code>)
+
|<tt><c-w>F</tt> || open in a new window (Ctrl-w <tt>F</tt>)
 
|-
 
|-
|<code><c-w>gF</code> || open in a new tab (Ctrl-w <code>gF</code>)
+
|<tt><c-w>gF</tt> || open in a new tab (Ctrl-w <tt>gF</tt>)
 
|}
 
|}
   
Line 58: Line 58:
 
*{{help|:cfile}}
 
*{{help|:cfile}}
   
The {{script|id=2184|text=file:line plugin}} allows you to use combinations of file name and line number, like <code>global.h:123</code>, as an argument to Vim. When you open ''file:line'', the script checks if ''file'' exists and ''line'' is a number. If so, Vim opens ''file'' at the correct ''line'' line number.
+
The {{script|id=2184|text=file:line plugin}} allows you to use combinations of file name and line number, like <tt>global.h:123</tt>, as an argument to Vim. When you open ''file:line'', the script checks if ''file'' exists and ''line'' is a number. If so, Vim opens ''file'' at the correct ''line'' line number.
   
 
==Names containing spaces==
 
==Names containing spaces==
With default settings, Vim regards spaces as delimiters (not part of a file name). That means you cannot type <code>gf</code> to open a file if the name contains a space character. However, you can visually select the name (including the spaces), then type <code>gf</code> to open the file. For example, you could position the cursor on the first character of the file name, then press <code>v</code> to start visual selection, then repeatedly press <code>E</code> to move to the end of the next WORD until the whole name is selected, then type <code>gf</code>.
+
With default settings, Vim regards spaces as delimiters (not part of a file name). That means you cannot type <tt>gf</tt> to open a file if the name contains a space character. However, you can visually select the name (including the spaces), then type <tt>gf</tt> to open the file. For example, you could position the cursor on the first character of the file name, then press <tt>v</tt> to start visual selection, then repeatedly press <tt>E</tt> to move to the end of the next WORD until the whole name is selected, then type <tt>gf</tt>.
   
 
As an example on a Windows system, the following line may identify a file:
 
As an example on a Windows system, the following line may identify a file:
Line 68: Line 68:
 
</pre>
 
</pre>
   
With the default settings, if you put the cursor on the "and" word then type <code>gf</code>, Vim will try to open a file called <code>and</code>. However, you can open the required file by selecting the whole name then typing <code>gf</code>. For example, type <code>^vg_</code> to select the name, then <code>gf</code> to open the file (<code>^</code> jumps to the first nonblank character; <code>v</code> enters visual mode; <code>g_</code> jumps to the last nonblank character).
+
With the default settings, if you put the cursor on the "and" word then type <tt>gf</tt>, Vim will try to open a file called <tt>and</tt>. However, you can open the required file by selecting the whole name then typing <tt>gf</tt>. For example, type <tt>^vg_</tt> to select the name, then <tt>gf</tt> to open the file (<tt>^</tt> jumps to the first nonblank character; <tt>v</tt> enters visual mode; <tt>g_</tt> jumps to the last nonblank character).
   
If you prefer the mouse, depending on your settings, you may be able to select the name with the mouse, then type <code>gf</code>. Before typing <code>gf</code>, check that you are in visual mode (if the status line shows <code>--SELECT--</code>, switch to visual mode by pressing Ctrl-g).
+
If you prefer the mouse, depending on your settings, you may be able to select the name with the mouse, then type <tt>gf</tt>. Before typing <tt>gf</tt>, check that you are in visual mode (if the status line shows <tt>--SELECT--</tt>, switch to visual mode by pressing Ctrl-g).
   
===Adjusting <code>isfname</code>===
+
===Adjusting <tt>isfname</tt>===
 
To have a space (ASCII 32) considered as a valid character for a file name, add the following to your [[vimrc]]:
 
To have a space (ASCII 32) considered as a valid character for a file name, add the following to your [[vimrc]]:
 
<pre>
 
<pre>
Line 78: Line 78:
 
</pre>
 
</pre>
   
On Windows, another approach is to use the 8.3 short path name to avoid spaces. For example, the following tells Vim to look for files in the <code>C:\Documents and Settings\My User Name\My Documents</code> directory:
+
On Windows, another approach is to use the 8.3 short path name to avoid spaces. For example, the following tells Vim to look for files in the <tt>C:\Documents and Settings\My User Name\My Documents</tt> directory:
 
<pre>
 
<pre>
 
:set path+=C:\\DOCUME~1\\MYUSER~1\\MYDOCU~1
 
:set path+=C:\\DOCUME~1\\MYUSER~1\\MYDOCU~1
 
</pre>
 
</pre>
   
Double backslashes are required in the <code>:set</code> command to get single backslashes in the value. Alternately, use forward slashes:
+
Double backslashes are required in the <tt>:set</tt> command to get single backslashes in the value. Alternately, use forward slashes:
 
<pre>
 
<pre>
 
:set path+=C:/DOCUME~1/MYUSER~1/MYDOCU~1
 
:set path+=C:/DOCUME~1/MYUSER~1/MYDOCU~1
Line 99: Line 99:
 
</pre>
 
</pre>
   
Assuming the file <code>abc.txt</code> exists in the <code>$mydir</code> directory, you can put the cursor on the name then type <code>gf</code> to open the file.
+
Assuming the file <tt>abc.txt</tt> exists in the <tt>$mydir</tt> directory, you can put the cursor on the name then type <tt>gf</tt> to open the file.
   
If you copy your files to another system using different directories, you can alter the definition of <code>$mydir</code>, for example:
+
If you copy your files to another system using different directories, you can alter the definition of <tt>$mydir</tt>, for example:
 
<pre>
 
<pre>
 
let $mydir = '/home/myname'
 
let $mydir = '/home/myname'
 
</pre>
 
</pre>
   
The above used forward slashes for the path delimiter; that should work on all systems. On Windows, you could use backslashes instead, and you can mix back and forward slashes (for example, using backslashes in <code>$mydir</code> and forward slashes in <code>$mydir/abc.txt</code>).
+
The above used forward slashes for the path delimiter; that should work on all systems. On Windows, you could use backslashes instead, and you can mix back and forward slashes (for example, using backslashes in <tt>$mydir</tt> and forward slashes in <tt>$mydir/abc.txt</tt>).
   
 
===Using selection===
 
===Using selection===
 
Just select the path and then use
 
Just select the path and then use
<code>gf</code> or <code><c-w>f</code> or <code><c-w>gf</code>
+
<tt>gf</tt> or <tt><c-w>f</tt> or <tt><c-w>gf</tt>
   
 
(This may need Vim 7.2+. Older versions haven't been tested yet.)
 
(This may need Vim 7.2+. Older versions haven't been tested yet.)
Line 138: Line 138:
 
==Comments==
 
==Comments==
 
{{todo}}
 
{{todo}}
*Need brief explanation <code>'isfname'</code>.
+
*Need brief explanation <tt>'isfname'</tt>.
*Need brief explanation of <code>'path'</code>: meanings of <code>.</code> and <code>,,</code>; how to include space in path.
+
*Need brief explanation of <tt>'path'</tt>: meanings of <tt>.</tt> and <tt>,,</tt>; how to include space in path.
   
 
----
 
----
Line 147: Line 147:
 
</pre>
 
</pre>
   
The plugin {{script|id=229|text=searchInRuntime}} overrides <code>gf</code> and <code>CTRL-W_f</code> to ask which file must be opened if several match. Alternatives for <code>:sp</code> and <code>:vsp</code> are also provided.
+
The plugin {{script|id=229|text=searchInRuntime}} overrides <tt>gf</tt> and <tt>CTRL-W_f</tt> to ask which file must be opened if several match. Alternatives for <tt>:sp</tt> and <tt>:vsp</tt> are also provided.
   
 
Note that if your path contains many directories or recursive searches, it can take a very long time to find all matches, and it might be better for you to just stop at the first file found.
 
Note that if your path contains many directories or recursive searches, it can take a very long time to find all matches, and it might be better for you to just stop at the first file found.
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)