Vim Tips Wiki
(we use capital letters with CTRL key combinations throughout the wiki to be consistent with Vim's help. Vim cannot tell the difference between shifted and unshifted letters combined with CTRL anyway.)
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Duplicate|193|1322}}
 
{{Duplicate|193|1322}}
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=530
 
|id=530
Line 7: Line 6:
 
|created=2003
 
|created=2003
 
|complexity=basic
 
|complexity=basic
|author=Geoff Hubbard
+
|author=
|version=5.7
+
|version=6.0
 
|rating=15/19
 
|rating=15/19
|category1=Duplicate
+
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
  +
Register <code>%</code> contains the name of the current file, and register <code>#</code> contains the name of the alternate file. These registers allow the name of the current or alternate files to be displayed or inserted. The name, or full path, or directory containing the file can be used.
To get the name of the file you are currently editing use:
 
<pre>
 
@%
 
</pre>
 
   
  +
For example, in directory <code>/abc</code> the command <code>vim def/my.txt</code> would edit file <code>/abc/def/my.txt</code>. The following commands could be entered to display the information shown.
If you want to make sure of the path as well use:
 
  +
{| class="cleartable"
<pre>
 
  +
| <code>:echo @% </code> || <code>def/my.txt</code> || directory/name of file
expand("%:p")
 
  +
|-
</pre>
 
  +
| <code>:echo expand('%:t') </code> || <code>my.txt</code> || name of file ('tail')
  +
|-
  +
| <code>:echo expand('%:p') </code> || <code>/abc/def/my.txt</code> || full path
  +
|-
  +
| <code>:echo expand('%:p:h')</code> || <code>/abc/def</code> || directory containing file ('head')
  +
|}
   
  +
If all that is wanted is to display the name of the current file, type Ctrl-G (or press <code>1</code> then Ctrl-G for the full path).
==References==
 
*{{help|@}}
 
*{{help|expand()}}
 
*{{help|cmdline-special}}
 
*[[VimTip600|Copy filename to clipboard]] shows why it might be useful
 
   
  +
When using <code>@%</code>, the name is displayed relative to the current directory.
==Comments==
 
To enter the name of the current file in insert or command mode, use <C-R>%.
 
   
  +
In insert mode, type Ctrl-R then <code>%</code> to insert the name of the current file.
----
 
If you want to use it as part of a command line just use %, for example:
 
!echo %
 
   
  +
In command mode (after typing a colon), type Ctrl-R then <code>%</code> to insert the name of the current file. The inserted name can then be edited to create a similar name.
----
 
Without the extention:
 
!echo %:r
 
   
  +
In normal mode, type <code>"%p</code> to put the name of the current file after the cursor (or <code>"%P</code> to insert the name before the cursor).
----
 
Ctrl-G is an easier way to get this info.
 
   
  +
The following commands insert lines consisting of the full path of the current and alternate files into the buffer:
----
 
 
<pre>
If you need to insert otherfile names (say the *.h name in an #include), you can do C-X+C-F for filename completion.
 
 
:put =expand('%:p')
  +
:put =expand('#:p')
 
</pre>
   
  +
==See also==
----
 
  +
:''Need to merge/clean.''
Complete path, filename and extension:
 
  +
*[[VimTip193|193 Insert current filename]]
2+Ctrl-G
 
  +
*[[VimTip432|432 Putting the current file on the Windows clipboard]]
 
*[[VimTip600|600 Copy filename to clipboard]]
  +
*[[VimTip891|891 Copy parts of filename to clipboard]]
   
 
==References==
----
 
 
*{{help|registers}}
It can be useful to get the name of the current file in command mode, and be able to edit it. The % sign can be used to get the name, but that will not allow you to edit the path. By pressing ^R#, the current path will be inserted, and you can edit it to open another file.
 
 
*{{help|expand()}}
 
*{{help|cmdline-special}}
   
 
==Comments==
 
If you need to insert other file names (say the *.h name in an #include), you can do C-X+C-F for filename completion.
   
 
----
 
----
 
Hey quick question, Ctrl-G worked but I'm wondering if there is a special way to examine the contents of the % register? According to {{help|@}}
 
  +
@{0-9a-z".=*}
Hey quick question, Ctrl-G worked but I'm wondering if there is a special way to examine the contents of the % register? According to the vim docs above
 
*@* @{0-9a-z".=*} Execute the contents of register {0-9a-z".=*} [count] times. Note that register '%' (name of the current file) and '#' (name of the alternate file) cannot be used. Is there a special way to echo its contents? I'm on vim version 7.3 with patches: 1-154.
+
Execute the contents of register {0-9a-z".=*} [count] times.
  +
Note that register '%' (name of the current file) and
  +
'#' (name of the alternate file) cannot be used.
  +
Is there a special way to echo its contents?
  +
:I have edited the tip to clarify several points, including the above. That help is for ''executing'' registers and does not apply when a register is displayed or inserted. [[User:JohnBeckett|JohnBeckett]] 03:06, November 11, 2011 (UTC)

Revision as of 17:41, 15 November 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 530 Printable Monobook Previous Next

created 2003 · complexity basic · version 6.0


Register % contains the name of the current file, and register # contains the name of the alternate file. These registers allow the name of the current or alternate files to be displayed or inserted. The name, or full path, or directory containing the file can be used.

For example, in directory /abc the command vim def/my.txt would edit file /abc/def/my.txt. The following commands could be entered to display the information shown.

:echo @% def/my.txt directory/name of file
:echo expand('%:t') my.txt name of file ('tail')
:echo expand('%:p') /abc/def/my.txt full path
:echo expand('%:p:h') /abc/def directory containing file ('head')

If all that is wanted is to display the name of the current file, type Ctrl-G (or press 1 then Ctrl-G for the full path).

When using @%, the name is displayed relative to the current directory.

In insert mode, type Ctrl-R then % to insert the name of the current file.

In command mode (after typing a colon), type Ctrl-R then % to insert the name of the current file. The inserted name can then be edited to create a similar name.

In normal mode, type "%p to put the name of the current file after the cursor (or "%P to insert the name before the cursor).

The following commands insert lines consisting of the full path of the current and alternate files into the buffer:

:put =expand('%:p')
:put =expand('#:p')

See also

Need to merge/clean.

References

Comments

If you need to insert other file names (say the *.h name in an #include), you can do C-X+C-F for filename completion.


Hey quick question, Ctrl-G worked but I'm wondering if there is a special way to examine the contents of the % register? According to :help @

@{0-9a-z".=*}
Execute the contents of register {0-9a-z".=*} [count] times.
Note that register '%' (name of the current file) and
'#' (name of the alternate file) cannot be used.

Is there a special way to echo its contents?

I have edited the tip to clarify several points, including the above. That help is for executing registers and does not apply when a register is displayed or inserted. JohnBeckett 03:06, November 11, 2011 (UTC)