Vim Tips Wiki
(Move categories to tip template)
(Add a more complex example showing that you can chain the modifiers.)
Tags: Visual edit apiedit
(19 intermediate revisions by 10 users not shown)
Line 1: Line 1:
  +
{{Duplicate|193|1322}}
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=530
 
|id=530
 
|previous=529
 
|previous=529
 
|next=531
 
|next=531
|created=August 13, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
|author=Geoff Hubbard
+
|author=
|version=5.7
+
|version=6.0
 
|rating=15/19
 
|rating=15/19
 
|category1=
 
|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:
 
   
  +
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.
@%
 
  +
{| class="cleartable"
  +
| <code>:echo @% </code> || <code>def/my.txt</code> || directory/name of file (relative to the current working directory of <code>/abc</code>)
  +
|-
  +
| <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')
  +
|-
  +
|<code>:echo expand('%:p:h:t')</code>
  +
|<code>def</code>
  +
|First get the full path with <code>:p</code> (<code>/abc/def/my.txt</code>), then get the head of that with <code>:h</code> (<code>/abc/def</code>), then get the tail of that with <code>:t</code> (<code>def</code>)
  +
|}
   
  +
For more info run <code>:help expand</code>
If you want to make sure of the path as well use:
 
   
  +
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).
expand("%:p")
 
  +
  +
When using <code>@%</code>, the name is displayed relative to the current directory.
  +
  +
In insert mode, type Ctrl-R then <code>%</code> to insert the name of the current file.
  +
  +
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.
  +
  +
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).
  +
  +
The following commands insert lines consisting of the full path of the current and alternate files into the buffer:
  +
<pre>
 
:put =expand('%:p')
  +
:put =expand('#:p')
  +
</pre>
  +
  +
==See also==
  +
:''Need to merge/clean.''
  +
*[[VimTip193|193 Insert current filename]]
  +
*[[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==
 
==References==
*{{help|@}}
+
*{{help|registers}}
 
*{{help|expand()}}
 
*{{help|expand()}}
 
*{{help|cmdline-special}}
 
*{{help|cmdline-special}}
See [[VimTip296]] for why it might be a useful thing to do.
 
   
 
==Comments==
 
==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.
To enter the name of the current file in insert or command mode, use &lt;C-R&gt;%.
 
 
----
 
If you want to use it as part of a command line just use %, for example:
 
!echo %
 
 
----
 
Ctrl-G is an easier way to get this info.
 
 
----
 
If you need to insert otherfile 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. [[User:JohnBeckett|JohnBeckett]] 03:06, November 11, 2011 (UTC)

Revision as of 20:42, 7 July 2015

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 (relative to the current working directory of /abc)
: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')
:echo expand('%:p:h:t') def First get the full path with :p (/abc/def/my.txt), then get the head of that with :h (/abc/def), then get the tail of that with :t (def)

For more info run :help expand

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)