What do % and ! mean in Vim commands?

vim
14,258

Solution 1

Within Vim, run :h :! and :h :% to know what each does.

Now, the :% is used to replace the contents of the file with the output of the shell command run using :!. If you don't want to touch the contents of the file, don't use %. Just do:

:!ls

Solution 2

According to VIM Tutorial:

%

Move cursor to the matching bracket.
Place cursor on {}[]() and type "%".

!

filter through external command 
Any UNIX command can be executed from the vi command line by typing an "!" before the UNIX command.
Autowrite can be intentionally avoided by using "!" to avoid the save when switching files.

for more information see Vim Commands Cheat Sheet, and VIM Tutorial

Solution 3

I had this same question and found this in :h cmdline-special, which was the meaning I was looking for:

%   Is replaced with the current file name.       *:_%* *c_%*

Solution 4

None of these answers is properly correct. At the beginning of a Vim command line statement, you can specify a range over which to operate. % is a common range for specifying the whole file, shorthand for 1,$, i.e. line 1 to end of file. See See :h cmdline-ranges for more.

Share:
14,258

Related videos on Youtube

vico
Author by

vico

Updated on September 18, 2022

Comments

  • vico
    vico over 1 year

    In Vim,

     :%!ls 
    

    executes ls command and prints its output to the current editable file.

    But what do % and ! mean separately in vim?

    Is it possible execute ls and not put its output to document?