Vim Tips Wiki
Register
(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.)
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
  +
{{Duplicate|193|1322}}
<div id="News" style="background-color: #ccffcc; margin: 0 1em 0 1em; padding: 0 10px; border: 1px solid #000;">
 
<big>'''Duplicate tip'''</big>
 
 
This tip is very similar to the following:
 
*[[Insert current filename]]
 
These tips need to be merged &ndash; see the [[Vim_Tips_Wiki:Merge_guidelines|merge guidelines]].
 
</div>
 
{{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
  +
|-
  +
| <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).
If you want to make sure of the path as well use:
 
   
  +
When using <code>@%</code>, the name is displayed relative to the current directory.
expand("%:p")
 
  +
  +
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}}
*[[VimTip600|Copy filename to clipboard]] shows why it might be useful
 
   
 
==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 <C-R>%.
 
 
----
 
If you want to use it as part of a command line just use %, for example:
 
!echo %
 
 
----
 
Without the extention:
 
!echo %:r
 
 
----
 
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|@}}
Complete path, filename and extension:
 
  +
@{0-9a-z".=*}
2+Ctrl-G
 
  +
Execute the contents of register {0-9a-z".=*} [count] times.
[[Category:Duplicate]]
 
  +
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)