:echo accepts a Vimscript expression, whereas:! accepts an external command, which is a special case for the file name, which is accepted :edit , etc.
For external commands and file names, there are special characters, such as % and # , described in the section :help cmdline-special . This also includes this key sentence:
In Ex commands, in places where you can use the file name , the following characters have special meaning.
In contrast :echo does not accept a file name, but an expression. There are several ways to resolve the current file name; the most direct - through expand() :
:echo expand('%')
Alternatively, since the current file name is also stored in the special register % , and the registers are addressed via @ sigil:
:echo @%
Another way
This also explains the frequent question of why :edit g:variable does not work as expected. Vim grading rules differ from most programming languages. To evaluate a variable (or expression) you need to use :execute ; otherwise it was taken literally; Those. Vim uses the variable name as an argument.
source share