0

I read the section of current-file in the document. (see :h current-file) And I have a question. if % stands for the current file name, why I can not use echo %?

2 Answers 2

4

So, you found :help current-file:

As long as you don't write the buffer, the original file remains unchanged.
If you start editing a file (read a file into the buffer), the file name is
remembered as the "current file name".  This is also known as the name of the
current buffer.  It can be used with "%" on the command line :_%.

Now, if you follow the tag to :help :_%, you get this very helpful paragraph:

In Ex commands, at places where a file name can be used, the following
characters have a special meaning.  These can also be used in the expression
function expand().

where the most salient informations seems to be (at this point):

at places where a file name can be used

Since you want to use :echo, the next logical step is to take a look at :help :echo, to see if it expects a filename:

:ec[ho] {expr1} ..  Echoes each {expr1}, with a space in between. [...]

It appears to expect an expression, not a filename, so you might want to go back to :help :_% with <C-t> and re-read that paragraph to see if you missed anything:

In Ex commands, at places where a file name can be used, the following
characters have a special meaning.  These can also be used in the expression
function expand().

where, now that you are armed with the notion of "expression", the following sentence suddenly makes a lot more sense:

These can also be used in the expression function expand()

Yes, :echo expects an expression and that expand() looks promising. From there, you should follow the tag to :help expand(), where everything becomes clearer and, frankly, very interesting. One cursory look at that section and a bit of experimenting should allow you to come up with the following in no time:

:echo expand('%')

And maybe, along the way, you will have noticed that the documentation for every Ex command follows the same pattern:

:command_name {type_of_argument}  Yadda-yadda

where {type_of_argument} instructs you about, well… what type of argument is expected: {expr} for expressions, {file} for files, etc. This is very helpful.

Now… you might think to yourself "But that's a lot to type, isn't there a better way to print out the current filename?".

Well yes, there are a few, actually.

One is to use @% with :echo, which is an expression that returns the text in register :help "%:

:echo @%

See :help expr-register.

Another one is simply to use :help :file, which literally exists for that exact purpose:

:file
2
  • And of course C-g. I've really appreciated this style of answer, which walks through how to understand the docs. Kudos.
    – D. Ben Knoble
    Oct 4, 2023 at 15:30
  • 1
    "Teach a man to fish" and all that.
    – romainl
    Oct 4, 2023 at 16:00
1

% stands for the current filename in most commands but the command parsing depends on the command (normal, map and echo are three command that exemplify that)

The second part of the echo command is a vimscript expression.

A rewrite of echo % is:

:echo expand('%')
1
  • I think something somewhere mentions this explicitly. Maybe on help {file}?
    – D. Ben Knoble
    Oct 3, 2023 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.