In Vim, I can echo the current filename using this command:
:echo @%
I found that information here: http://vim.wikia.com/wiki/Get_the_name_of_the_current_file
Can someone explain why the @
symbol is necessary? If I enter the command without the @
symbol, I get an error:
E15: Invalid expression: %
E15: Invalid expression: %
However, if I try to send the filename to a bang
command as an argument, including the @
sign appears as a regular character in the argument. Removing the @
sign works. In other words, in my .bash_profile
I have the following function:
test_func() {
echo $1
}
In Vim, I run:
:! test_func @% #outputs @path/to/my/file
:! test_func % #outputs path/to/my/file
What is the @
symbol doing and why does it behave differently when sending the output to a bash function?
I believe that is :h expr-register
:
register expr-register @r
@r contents of register 'r'
The result is the contents of the named register, as a single string. Newlines are inserted where required. To get the contents of the unnamed register use @" or @@. See |registers| for an explanation of the available registers.
When using the '=' register you get the expression itself, not what it evaluates to. Use |eval()| to evaluate it.
As to why you don't need that for :!
that is probably because of :h cmdline-special
.
- Ex special characters cmdline-special
Note: These are special characters in the executed command line. If you want to insert special things while typing you can use the CTRL-R command. For example, "%" stands for the current file name, while CTRL-R % inserts the current file name right away. See |c_CTRL-R|.
Note: If you want to avoid the special characters in a Vim script you may want to use |fnameescape()|.
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() |expand()|. % Is replaced with the current file name. :_% c_%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With