What is "%:r" in vimrc file?

Viewed 1575
  1. What is %:r in vimrc file?
  2. what is difference between :gcc and :!gcc ?
  3. what is % in vimrc file?

I'm studying vi/vim...but it's hard for me. I want information about vimrc operations. where can I get it??

I really appreciate all answers.

2 Answers

Let's answer your questions in order…

  1. What is difference between :gcc and :!gcc?

    :something is an ex command. You can find a complete list of available ex commands under :help ex-cmd-index and look for help on a specific command with :help :something.

    :gcc is not an existing ex command.

    :!something calls the external command something so :!gcc would call gcc.

  2. What is % in vimrc file?

    It's not anything specific to your vimrc. When used as argument to an external command, it represents the current file name. Assuming the current file name is foobar.c, :!gcc % is expanded to gcc foobar.c before being passed to the shell.

    See :help c_%.

  3. What is %:r in vimrc file?

    Again, it's not anything specific to your vimrc. :r is a file name modifier applied to the current file name. Assuming the current file name is foobar.c, %:r would be expanded to foobar.

    See :help filename-modifiers.

It looks like you are trying to make sense of a command similar to:

:!gcc % -o %:r

which, again assuming the current file name is foobar.c, should be expanded to:

gcc foobar.c -o foobar

before being sent to your shell… and result in a foobar executable right beside your foobar.c.

  1. Before explaining %:r we need to know what % refers to. The "%" sign in the vim command refers to the current file. For example what if you wanted to write a vim command to compile the current file which say is a cpp file, then you'd do something like g++ -o %:r.out. Here % refers to the current file that you're working with.
  2. :gcc means that you're trying to run the command inside the vim editor and :!gcc means that whatever you're typing after the exclamation will be run in your terminal.
  3. Explained the % so now what is :r? Well, :r is used to read the contents of another file %:r. Where %:r.out refers to a file that is renamed just by appending .out at the end of the name of the %(current) file. So if you do something like :!./%r.out then this runs/executes the compiled file which you had already created.
Related