Vim syntax for getting current file name

In Vim, I can repeat the current file name using the following command:

:echo @% 

I found this information here: http://vim.wikia.com/wiki/Get_the_name_of_the_current_file

Can someone explain why the @ character is needed? If I enter a command without the @ character, I get an error message:

 E15: Invalid expression: % E15: Invalid expression: % 

However, if I try to send the file name to the bang command as an argument, including the @ sign, will appear as a regular character in the argument. Delete the @ sign. 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 does the @ character do and why does it behave differently when sending output to a bash function?

+5
source share
2 answers

I believe this is :h expr-register :

register expr-register @r


@r contents of register 'r'

The result is the contents of the named register as one line. New lines are inserted where necessary. To get the contents of an 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. Use | eval () | rate it.

What you do not need for :! probably due to :h cmdline-special .

  1. Ex special characters cmdline-special

Note. These are special characters on the command line. If you want to insert special things during input, you can use the CTRL-R command. For example, "%" indicates the current file name, while CTRL-R% inserts the current file name immediately. See | C_CTRL-R |.

Note. If you want to avoid special characters in a Vim script, you might want to use | fnameescape () |.

In Ex commands in places where a file name can be used, the following characters have special meaning. They can also be used in a function expand () | expand () |. % Replaced with the current file name .: _% c_%

+3
source

: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.

+2
source

Source: https://habr.com/ru/post/1213735/


All Articles