1

When I added a vim plugin, VimAwesome document said that :source %. What does this mean? I'd like to understand % meaning.

3

In vim, % expands to the name (path) of the file opened in the current buffer. In this case, it seems the file in the current buffer is the script that implements VimAwesome.

The :source command will read the file and interpret the VimScript instructions in it, so this command will effectively load the vim plug-in into your current vim session.

2
  • Thanks for simple answer. In other words, is :source % the same meaning as :source ~/.vimrc in this case?
    – y_natsui
    Sep 2 '18 at 17:31
  • Yes, that's correct, if ~/.vimrc is the file you have open, then that's what % will expand to.
    – filbranden
    Sep 2 '18 at 19:49
1

vim uses vi's keys for the % (current file/buffer) and # (alternate file/buffer).

In any vi-(imitation/emulation/clone) you can use those characters for referring to the current filename and the "other" file. On most terminals/keyboards you can use controlshift6 for switching between current/alternate buffers, although that is not mentioned in POSIX vi. It only goes as far as this:

Non- <backslash>-escaped '%' characters appearing in file arguments to any ex command shall be replaced by the current pathname; unescaped '#' characters shall be replaced by the alternate pathname. It shall be an error if '%' or '#' characters appear unescaped in an argument and their corresponding values are not set.

1

vi already supported special symbols as shortcuts to certain files in the command-line. Vim builds on top of that; you'll find the information at :help cmdline-special.

The % symbol in particular refers to the current buffer's filespec. There's also an eponymous register (:help quote_%), so you can alternatively insert the value into the command-line via <C-r>%. Or, as the first help topic mentions, you can use this to print the resolved value:

:echo expand('%')

The :source % command, when used on the ~/.vimrc configuration, will reload the config. You can achieve the same effect by quitting and restarting Vim, but this is faster. Note that your Vim configuration needs to be cleanly written for that to work. If not, :autocmds may be duplicated, or you might get errors about existing mappings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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